Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get proper layout with "fill_parent"

I'm trying to create this simple layout in Android.

enter image description here

A should wrap to fit its content and left|center_vertical align.

B should expand as much as possible, filling all empty space.

C should be right-aligned, wrapping to fill its content and also being aligned center_vertical.

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="left|center_vertical">
    <!-- A -->
    <ImageView android:id="@+id/example_item_icon"
            android:layout_width="48px"
            android:layout_height="48px"/>
    <!-- B -->
    <LinearLayout android:orientation="vertical"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:gravity="left|center_vertical"
             android:padding="5px">

        <TextView android:id="@+id/example_item_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"/>

        <TextView android:id="@+id/example_item_level_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold|italic"
                android:lines="1"
                android:textSize="10px"/>
    </LinearLayout>
    <!-- C -->
    <TextView android:id="@+id/example_item_count_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="14px"/>
</LinearLayout>

In this layout, C is pushed off-screen. How can I make this layout work?

like image 591
Naftuli Kay Avatar asked Mar 10 '11 00:03

Naftuli Kay


People also ask

How to include layout in android?

To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

What is the difference between Match_parent and Fill_parent?

Both have similar functionality only difference is that fill_parent is used up to API level 8 and match_parent is used after API level 8 or higher level.

What is Fill_parent in Android layout?

FILL_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. This value is deprecated starting in API Level 8 and replaced by MATCH_PARENT .


2 Answers

fill_parent means "be as big as the parent," not "use the remaining empty space." Instead of fill_parent, just use android:layout_width="0dip" and android:layout_weight="1".

like image 194
Romain Guy Avatar answered Oct 14 '22 06:10

Romain Guy


the trick, use a relative layout ... here's an example (off the top of me head - not checked and missing height fields and vertical align settings to make it easy to see the important stuff).

<relativelayout android:width="fillParent">
    <textview android:id="@+id/left" android:text="left" 
              android:width="wrap_content"
              android:layout_alignParentleft="true" />
    <textview android:id="@+id/right" android:text="right"
              android:width="wrap_content"
              android:layout_alignParentright="true" />
    <textview android:id="@+id/middle" android:text="middle"
              android:width="wrap_content"
              android:layout_alignleft="@id/right" android:layout_alignright="@id/left" />
</relativelayout>

edit: here it is with the typos cleaned (and it is tested as working)

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView
    android:id="@+id/left"
    android:text="left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" />
<TextView
    android:id="@+id/right"
    android:text="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />
<TextView
    android:id="@+id/middle"
    android:text="middle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/right"
    android:layout_toRightOf="@id/left" />
</RelativeLayout>
like image 30
SteelBytes Avatar answered Oct 14 '22 06:10

SteelBytes