有没有更好的方法来做这个布局?也许使用约束布局?
Is there a better way to do this layout ? Maybe using Constraint Layout?
是否有更简单(更好)的方式来进行此布局?也许使用 ConstraintLayout?
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>
对于这类场景,您可以使用带权重的 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>
使用 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>
是否有更简单(更好)的方式来进行此布局?也许使用 ConstraintLayout?
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>
对于这类场景,您可以使用带权重的 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>
使用 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>