我试图让所有的按钮都是相同的大小,并且都在屏幕的右侧,无论文本视图中有多少文本。
现在,按钮紧挨着垂直的LinearLayout (由2个TextViews组成),并且只有当其中一个TextViews超过一行时才会出现在正确的位置。
我尝试过将按钮添加到LinearLayout并给它一个权重,给每个TextViews一个权重,只给文本填充的LinearLayout一个权重,等等都没有用。
我现在想知道这是不是有问题,因为这不是主要的XML,而是一个重复的项目?但我不认为这会影响到它。
This is what it currently looks like
这是行项的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
发布于 2018-06-13 06:45:26
有很多不同的布局可以用来实现你想要的设计。您可以通过将外部布局元素从LinearLayout
更改为RelativeLayout
来使用这种方法
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Button" />
</RelativeLayout>
请注意,您只需添加以下属性:
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button"
添加到内部LinearLayout
,并将此属性添加到Button
视图:
android:layout_alignParentEnd="true"
您可以添加Margin
属性来为视图提供一些额外的空间。
发布于 2018-06-13 06:11:46
这就是您的xml应该满足您的需求
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
发布于 2018-06-13 06:29:50
您也可以使用ConstraintLayout来实现这一点
<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/header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Header"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Second text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/header"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
https://stackoverflow.com/questions/50826487
复制相似问题