简体   繁体   中英

how to align height of LinearLayout inside other Layout

I have following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <RelativeLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:orientation="horizontal">
             <ImageButton
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:src="@drawable/ic_btn_find_prev"
                 android:contentDescription=""/>
             <LinearLayout
                    android:id="@+id/fragment_counter"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_red_dark"
                    android:gravity="center_vertical|center_horizontal"/>
             <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_btn_find_next"
                    android:layout_alignParentRight="true"
                    android:contentDescription=""/>
    </RelativeLayout>

</RelativeLayout>

and following ui result:

在此处输入图片说明

I would like to have red layout with height like YELLOW LINE has. If I set height like match_parent or fill_parent red layout fills all screen. Please help me to receive correct result.

EDITED I use LinearLayout (@+id/fragment_counter) for programmatic way of adding elements.

final TextView item = new TextView(this);
item.setGravity(Gravity.CENTER);
item.setBackgroundResource(R.drawable.shape);
item.setText("1");
final RelativeLayout.LayoutParams params = new     RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
params.setMargins(5, 0, 5, 0);
item.setLayoutParams(params);

From example: item "1" was added and should be centered in layout. If red layout aling like YELLOW line item "1" will be centered correctly.

Set the RelativeLayout layout_height attribute to a specific height (relative to the size of your button drawables) and use fill_parent on the LinearLayout layout_height attribute.

Also assuming you want the buttons on top you need to reorder that layout. And don't forget to set your orientation for the LinearLayout too.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/fragment_counter"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/holo_red_dark"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription=""
        android:src="@drawable/ic_btn_find_prev" />


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:contentDescription=""
        android:src="@drawable/ic_btn_find_next" />
</RelativeLayout>

Try this...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:textSize="36sp" />

   <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_red_dark"
        android:orientation="horizontal" >

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:contentDescription=""
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/fragment_counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:layout_centerInParent="true"
            android:background="#404040"
            android:gravity="center_vertical|center_horizontal"
            android:text="FRAG COUNT: 2"
            android:textColor="#FFFFFF" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:contentDescription=""
            android:src="@drawable/ic_launcher" />
    </RelativeLayout></RelativeLayout>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM