使用 wrap_content 拆分相对布局

Split a Relative Layout with wrap_content

我有一个 RelativeLayout 这样的:

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

在这个Layout里面,有一些TextView之类的东西,通过这些东西来定义Layout的高度,因为它设置为wrap_content,就像你上面看到的那样。

现在我想在 RelativeLayout 中有两个共享 space(相对于宽度)但填充整个布局的视图。这背后的目的是,我想要两个 onClickListener。换句话说:我想将布局拆分为两个相邻的视图(水平)。

我试过像这样在 RelativeLayout 中放置一个 LinearLayout:

<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>

RelativeLayout的全部,一个TextView占左边50%,一个占右边50%。这正是我想要的。 但是我也想让他们占据整个高度。

我不能做什么:将 LinearLayout 的高度设置为 match_parent。这是不可能的,因为整个东西都在另一个布局中,这将调整与此布局相关的高度。

编辑:这是我的新方法

<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>

不幸的是,这仍然不起作用。

编辑: 这是我想要的草图。第一张图片是布局,第二张图片显示了相同的布局,蓝色和红色视图。这些视图是我尝试创建的视图。

试着看看 ConstraintLayout

虽然最初有点吓人,但它可以完成所有其他布局可以做的一切,甚至更多(包括您刚才使用 "match_constraint" 提出的问题)。

它也是支持库的一部分,因此可用于较旧的项目。

所以主布局内部有三个视图,两个视图上方有 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>

如果我没理解错的话,您想要这两个 Views 是为了在它们上面设置 OnClickListener。这就是我的处理方式:

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

    <TextView
        android:id="@+id/togoTrue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pickup"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/togoSwitch"
        app:layout_constraintTop_toTopOf="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"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/togoTrue"
        app:layout_constraintRight_toLeftOf="@id/togoFalse"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/togoFalse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vor Ort"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/togoSwitch"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

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

</android.support.constraint.ConstraintLayout>