Android 使用换行内容拆分相对布局

Android 使用换行内容拆分相对布局,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我得到了一个类似这样的RelativeLayout: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_c

我得到了一个类似这样的
RelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<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\u 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>

试试看

虽然最初有点吓人,但它完成了所有其他布局所能做的一切,甚至更多(包括您刚才使用“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>

如果我理解正确,您需要这两个
视图
来设置
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>


不知何故,即使这样也没有成功。也许我做错了什么。我将RelativeLayout更改为ConstraintLayout,并在我的LinearLayout中设置了
app:layout\u constraintTop\u toTopOf=“parent”
app:layout\u constraintBottom\u toBottomOf=“parent”
。我假设您不能使用match\u parent,因为您想在垂直方向上占据父视图和另一视图之间的所有空间。如果使用视觉编辑器进行约束,请将线性布局的底部锚定到其他视图,然后将高度设置为“匹配约束”。如果在
相对视图中有其他
视图
,则在更改为
约束视图
时,还必须约束它们。
ConstraintLayout
的目标之一是展平视图层次结构,从而减少嵌套布局。
ConstraintLayout
中的约束仅对其直接子项有效,在这种情况下,您不会将
textview
包装在
LinearLayout
中,而只将
textview
自身约束到其他
视图和父视图。如果您需要更多有关转换为
ConstraintLayout
的帮助,请使用整个布局更新您的问题。@plaskoff请参阅我的edit@progNewbie你能画出你想要的样品图片吗?“一幅画能写千言万语”。@死鱼:请看我的画edit@progNewbie好的,你能粘贴你的实际布局吗?