我试图让按钮的大小都相同,并且都在屏幕的右侧,无论文本视图中有多少文本。
现在,按钮位于垂直 LinearLayout(由 2 个 TextView 组成)旁边,并且仅在其中一个 TextView 超过一行时才位于正确的位置。
我曾尝试将 Button 添加到 LinearLayout 并赋予它一个权重,为每个 TextViews 赋予一个权重,仅赋予文本填充的 LinearLayout 一个权重,等等都无济于事。
我现在想知道这是否是一个问题,因为这不是主要的 XML,而是一个重复的项目?但我不认为这会影响它。
这是行项目的 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" />
您可以使用许多不同的布局来实现您想要的设计。这是一种方法,您可以通过将外部 Layout 元素从 更改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
属性为视图提供一些额外的空间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句