【发布时间】:2012-10-27 09:02:37
【问题描述】:
我需要创建一个带有两个表单小部件(例如 - 两个按钮)的简单布局。第一个必须填满父布局的所有可用宽度,第二个必须有一些固定大小。
这就是我需要的:
如果我将 FILL_PARENT 设置为第一个小部件 - 我看不到第二个小部件。它只是从布局的视图区域吹走 :) 我不知道如何解决这个问题...
【问题讨论】:
我需要创建一个带有两个表单小部件(例如 - 两个按钮)的简单布局。第一个必须填满父布局的所有可用宽度,第二个必须有一些固定大小。
这就是我需要的:
如果我将 FILL_PARENT 设置为第一个小部件 - 我看不到第二个小部件。它只是从布局的视图区域吹走 :) 我不知道如何解决这个问题...
【问题讨论】:
最简单的方法是使用layout_weight 和LinearLayout。注意第一个TextView的宽度是"0dp",意思是“忽略我,使用权重”。权重可以是任意数字;因为它是唯一的加权视图,它会扩展以填满可用空间。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:layout_width="25dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
【讨论】:
您可以使用 RelativeLayout 或 FrameLayout 来实现。
<?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="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:background="#ccccee"
android:text="A label. I need to fill all available width." />
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingRight="5dp"
android:background="#aaddee"
android:text=">>" />
</RelativeLayout>
【讨论】: