首页 > 解决方案 > Split a Relative Layout with wrap_content

问题描述

I got a RelativeLayout like this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

Inside this Layout, there are some TextView and other stuff, through which the height of the Layout is defined, because it is set to wrap_content like you can see above.

Now I want to have have two Views in the RelativeLayout who share the space(in respect to width) but fill the whole Layout. The purpose behind this is, that I want to have two onClickListener. In other words: I want to kind of split the layout in two Views next to another (horizontally).

I tried to put a LinearLayout inside the RelativeLayout like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">
    <TextView
        android:id="@+id/togoTrueTrigger"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/togoFalseTrigger"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

It takes the whole with of the RelativeLayout and one TextView takes the left 50% and the other one the right 50%. That is exactly what I want. BUT I also want them to take the whole Height.

What I can't do: Set the Height of the LinearLayout to match_parent. This is not possible, because the whole thing is inside another layout and this would adjust the Height in relation to this layout.

EDIT: This is my new approach

<android.support.constraint.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="15dp">

                        <TextView
                            android:id="@+id/togoTrue"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Pickup"
                            android:textAppearance="@style/itemConfiguration"
                            app:layout_constraintLeft_toLeftOf="parent"/>

                        <com.bhargavms.podslider.PodSlider
                            android:id="@+id/togoSwitch"
                            android:layout_width="75dp"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            app:numberOfPods="2"
                            app:selectedPodColor="@color/colorAccent"
                            app:mainSliderColor="@color/colorPrimary"
                            app:podColor="#ffffff"
                            android:layout_centerInParent="true"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintBottom_toBottomOf="parent"/>

                        <TextView
                            android:id="@+id/togoFalse"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Vor Ort"
                            android:textAppearance="@style/itemConfiguration"
                            app:layout_constraintRight_toRightOf="parent"/>


                        <View
                            android:id="@+id/togoTrueTrigger"
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            app:layout_constraintWidth_percent="0.5"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintBottom_toBottomOf="parent"/>

                        <View
                            android:id="@+id/togoFalseTrigger"
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            app:layout_constraintWidth_percent="0.5"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintBottom_toBottomOf="parent"/>

                    </android.support.constraint.ConstraintLayout>

Unfortunately this still doesn't work.

EDIT: Here is a sketch of what I want. The first picture is the layout and the second shows the same layout with a blue and a red view. These Views are the ones I try to create.

enter image description here

标签: androidandroid-layoutandroid-constraintlayout

解决方案


所以主布局内部有三个视图和两个宽度为 50% 的视图。我相信这是你的答案:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/togoTrue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pickup"
        android:textAppearance="@style/itemConfiguration"
        app:layout_constraintLeft_toLeftOf="parent" />

    <com.bhargavms.podslider.PodSlider
        android:id="@+id/togoSwitch"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mainSliderColor="@color/colorPrimary"
        app:numberOfPods="2"
        app:podColor="#ffffff"
        app:selectedPodColor="@color/colorAccent" />

    <TextView
        android:id="@+id/togoFalse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vor Ort"
        android:textAppearance="@style/itemConfiguration"
        app:layout_constraintRight_toRightOf="parent" />


    <View
        android:id="@+id/togoTrueTrigger"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#44ffff00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent=".5" />

    <View
        android:id="@+id/togoFalseTrigger"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4400ff00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent=".5" />

</android.support.constraint.ConstraintLayout>

推荐阅读