我正在尝试创建以下布局。
+---------------------+
| Text View |
+---------------------+
| |
| |
| Custom View |
| Expands to |
| extra space |
| |
+---------------------+
|Button1 |Button2 |
+---------------------+
我的布局xml如下所示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="This is test text." android:id="@+id/statusTxt"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/gameView"
android:layout_gravity="bottom">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/gameButtonView">
<Button android:text="Done" android:id="@+id/doneBtn"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Undo" android:id="@+id/undoBtn"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
比以编程方式我要添加我的自定义视图
LinearLayout gameView = (LinearLayout)this.findViewById(R.id.gameView);
gameView.addView(gameBoardView, 1);
使用当前布局,它最终看起来像这样。
+---------------------+
| Text View |
+---------------------+
|Button1 |Button2 |
+---------------------+
| |
| |
| Custom View |
| Expands to |
| extra space |
| |
+---------------------+
当前布局的作用是将底部按钮粘贴在“自定义视图”上方。关于如何获取它以呈现我的描述的任何想法?
最佳答案
使用RelativeLayout。将您的TextView设置为alignParentTop =“ true”。将您的按钮放入alignParentBottom =“ true”的LinearLayout中,然后将中间的内容设置为上层=“ buttons”和下层=“ text”
像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Text at the top -->
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<!-- Your game play view -->
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/buttons"
android:layout_below="@+id/text" />
<!-- Buttons at the bottom -->
<LinearLayout
android:id="@+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>