在线性布局中将一个文本视图与另一个文本视图对齐
aligning one textview with the other in a linear layout
作为 recyclerview 的布局,我有一个文本视图并排放置(水平)。我将两个文本视图嵌套在一个线性布局中。我已经将两个文本视图都设置为扩展它们的高度以适应文本换行。我需要的是在只有一个需要时扩展两个文本视图的高度。我当前的布局是否有可以实现此目的的配置。
这是我的 xml:
<Linearlayout
android:id="@+id/view_foreground"
android:layout_width="0dp"
android:weightSum="2"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation: Horizontal>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:singleLine="false"
android:layout_weight="1"/>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:singleLine="false"
android:layout_weight="1"/>
</linearlayout>
我想你想做这样的事情:
<LinearLayout
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_weight="1"/>
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_weight="1"/>
</LinearLayout>
使用重力属性对齐文本视图 - 这是您期望的代码。
<Linearlayout
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/txt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:padding="10dp"
android:text="Your text goes here."
android:gravity="center|left"
android:layout_weight="1"/>
<TextView
android:id="@+id/txt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:padding="10dp"
android:text="Your text goes here."
android:gravity="center|left"
android:layout_weight="1"/>
</Linearlayout>
如果您为 LinearLayout 设置水平方向,则必须为视图设置 android:layout_width="0dp"
(如果您赋予它们权重)而不是 android:layout_height="0dp"
。我做了一些修改:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/txt1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="false" />
<TextView
android:id="@+id/txt2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="false" />
</LinearLayout>
作为 recyclerview 的布局,我有一个文本视图并排放置(水平)。我将两个文本视图嵌套在一个线性布局中。我已经将两个文本视图都设置为扩展它们的高度以适应文本换行。我需要的是在只有一个需要时扩展两个文本视图的高度。我当前的布局是否有可以实现此目的的配置。
这是我的 xml:
<Linearlayout
android:id="@+id/view_foreground"
android:layout_width="0dp"
android:weightSum="2"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation: Horizontal>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:singleLine="false"
android:layout_weight="1"/>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:singleLine="false"
android:layout_weight="1"/>
</linearlayout>
我想你想做这样的事情:
<LinearLayout
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_weight="1"/>
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_weight="1"/>
</LinearLayout>
使用重力属性对齐文本视图 - 这是您期望的代码。
<Linearlayout
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/txt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:padding="10dp"
android:text="Your text goes here."
android:gravity="center|left"
android:layout_weight="1"/>
<TextView
android:id="@+id/txt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:padding="10dp"
android:text="Your text goes here."
android:gravity="center|left"
android:layout_weight="1"/>
</Linearlayout>
如果您为 LinearLayout 设置水平方向,则必须为视图设置 android:layout_width="0dp"
(如果您赋予它们权重)而不是 android:layout_height="0dp"
。我做了一些修改:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/txt1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="false" />
<TextView
android:id="@+id/txt2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="false" />
</LinearLayout>