开发者

Android: Layout with middle component expand

开发者 https://www.devze.com 2023-04-05 19:38 出处:网络
I am trying to create the following layout. +---------------------+ |Text View| +---------------------+

I am trying to create the following layout.

+---------------------+
|     Text View       |
+---------------------+
|                     |
|                     |
|    Custom View      |
|    Expands to       |
|    extra space      |
|                     |
+---------------------+
|Button1   |Button2   |
+---------------------+

My layout xml looks like the following.

<?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>

Than programatically I am adding my custom view

LinearLayout gameView = (LinearLayout)this.findViewById(R.id.gameView);
gameView.addView(gameBoardView, 1);

With the current layout it ends up looking like this.

+---------------------+
|     Text View       |
+---------------------+
|Button1   |Button2   |
+---------------------+
|                     |
|                     |
|    Custom View      |
|    Expands to       |
|    extra space      |
|                     |
+---------------------+

What this current layout does is it sticks the bottom buttons above the Custom View. Any ideas on开发者_如何学C how I can get it to render how I have described?


Use a RelativeLayout. Set your TextView to alignParentTop="true". Put your buttons in a LinearLayout with alignParentBottom="true" and then set your content in the middle to above="buttons" and below="text"

Something like this:

<?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>
0

精彩评论

暂无评论...
验证码 换一张
取 消