Epi*_*aos 1 android android-layout android-linearlayout
我正在尝试创建以下布局.
+---------------------+
| Text View |
+---------------------+
| |
| |
| Custom View |
| Expands to |
| extra space |
| |
+---------------------+
|Button1 |Button2 |
+---------------------+
Run Code Online (Sandbox Code Playgroud)
我的布局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>
Run Code Online (Sandbox Code Playgroud)
比编程我添加自定义视图
LinearLayout gameView = (LinearLayout)this.findViewById(R.id.gameView);
gameView.addView(gameBoardView, 1);
Run Code Online (Sandbox Code Playgroud)
目前的布局最终看起来像这样.
+---------------------+
| Text View |
+---------------------+
|Button1 |Button2 |
+---------------------+
| |
| |
| Custom View |
| Expands to |
| extra space |
| |
+---------------------+
Run Code Online (Sandbox Code Playgroud)
当前布局的作用是将底部按钮固定在自定义视图上方.关于如何让它呈现我所描述的任何想法?
使用RelativeLayout.将TextView设置为alignParentTop ="true".将您的按钮放在LinearLayout中,并使用alignParentBottom ="true",然后将您的内容设置在中间位于上方="按钮"和下方="文本"
像这样的东西:
<?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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2560 次 |
| 最近记录: |