Android: How to create this simple layout?

2020-03-03 05:14发布

I need to create a simple layout with two form widgets (for example - two buttons). The first one must fill all available width of the parent layout and the second one must have some fixed size.

That's what I need: enter image description here

If I set FILL_PARENT to the first widget - I don't see the second one. It just blows away from a view area of the layout :) I don't know how to fix this...

2条回答
何必那么认真
2楼-- · 2020-03-03 05:47

You can accomplish that with a RelativeLayout or a 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>

How the layout looks

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-03-03 06:00

The easiest way to do this is using layout_weight with LinearLayout. Notice the width of the first TextView is "0dp", which means "ignore me and use the weight". The weight can be any number; since it's the only weighted view, it will expand to fill available space.

<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>
查看更多
登录 后发表回答