开发者

Layout Help, Disappearing LinearLayouts

开发者 https://www.devze.com 2023-03-16 03:35 出处:网络
I\'m trying to design a simple layout with 2 buttons in a horizontal linear layout centered at the bottom of the page (so layout_gravity=\"bottom\").And I also need a vertical linear layout that\'s ce

I'm trying to design a simple layout with 2 buttons in a horizontal linear layout centered at the bottom of the page (so layout_gravity="bottom"). And I also need a vertical linear layout that's centered in the screen that can hold 3 TextViews (that are implemented later in the program, so just need a LinearLayout to be ABLE to hold 3 TextViews).

I can get the buttons to their right spots (even though I would like them more seperated), but every time I try to make the TextViews LinearLayout it either doesn't appear or makes the other one disappear. I've been trying to get this down for awhile...can anybody help? Thanks

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/background">
<LinearLayout
    android:id="@+id/game_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:layout_margin="100dip">
    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_label"
        android:layout_gravity="center"/>
    <Button
        android:id="@+id/repeat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/repeat_label"
        android开发者_高级运维:layout_gravity="center"/>   
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_weight="1">
</LinearLayout>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_width="match_parent" android:orientation="vertical"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:gravity="center">
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_gravity="bottom" android:gravity="center">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button" android:id="@+id/button2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
</LinearLayout>

linearlayout2 is vertical layout with children positioned in the center

Is that what you need?

Layout Help, Disappearing LinearLayouts

EditText with "Some text" value is added through java with both LayoutParams set to WRAP_CONTENT


Make sure the horizontal LinearLayout of 2 Buttons has its height set to wrap_content. Also you can take a RelativeLayoutand keep two LinearLayout of vertical and horizontal orientation respectively.

This code shall suffice:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background">
    <LinearLayout
        android:id="@+id/game_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignparentbottom="true"
        android:layout_margin="100dip">
        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_label"
            android:layout_gravity="center"/>
        <Button
            android:id="@+id/repeat_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/repeat_label"
            android:layout_gravity="center"/>   
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerinparent="true"
        android:layout_weight="1">
    </LinearLayout>
</RelativeLayout>

Caveat: casing of properties could be different so correct them.


Quick idea:

Set the vertical LinearLayout's height to 0dp, and use weight to give it the rest of all the remaining space after your other view is laid out, e.g. give it android:layout_weight="1"

The tricky part is needing to set the height to 0 so it actually uses the layout_weight.

Caveat: It's late and I've had a couple glasses of wine. :)

0

精彩评论

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