2

有没有更简单(更好)的方法来做这个布局?也许使用约束布局?

3333 必须始终位于右下角,而 2222 必须始终位于 1111 下方。

不确定是否有更好的方法来使用单个 RelativeLayout 或 FrameLayout?

我已经尝试过约束布局,但 Android Studio 在移动元素时会变得有点疯狂。

屏幕截图布局

  <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

<LinearLayout
    android:id="@+id/LeftLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:layout_toStartOf="@+id/textView3"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:text="11111111111111111111111111111111111111111111111111111111" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="222222222"
        android:visibility="visible" />

</LinearLayout>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3333"
    android:layout_alignBottom="@+id/LeftLayout"
    android:layout_alignParentEnd="true" />       
</RelativeLayout>
4

2 回答 2

2

使用 ConstraintLayout,您可以创建包含“111”的文本视图,其宽度设置为MATCH_CONSTRAINT(0dp),高度设置为 maxLines 和WRAP_CONTENT. 然后将其约束到父级的左侧和顶部,并在右侧约束到包含“3333”的视图。带有“222”的视图被简单地垂直限制在“111”的底部,并且在父视图的左侧。“333”然后简单地被限制在父级的底部和右侧。

像这样

<?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:padding="8dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="1111111111111111111111111111111111111111111111111111111111"
        android:maxLines="2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView8" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2222222222"
        app:layout_constraintTop_toBottomOf="@+id/textView7"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3333"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
于 2016-10-13T20:05:01.357 回答
1

对于这类场景,您可以使用带有权重的 LinearLayout。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="11111111111111111111111111111111111111111" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="2222222222" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom" 
        android:text="3333" />
</LinearLayout>
于 2016-10-13T18:39:38.377 回答