Android ListView: Adjust component height to the height of your parent layout

3

I have the following design to complete for items in a ListView

the 3 bars on the right side are indicative that mean something to the user.

The detail is that I can not get the bars to adjust to the height of the item (as required by the figure), I have achieved the following:

with this xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp"
            android:layout_centerVertical="true">

            <TextView
                android:id="@+id/txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name" />

            <TextView
                android:id="@+id/txt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/relativeLayout2"
                android:text="@string/app_name"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/img_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="29dp"
            android:src="@drawable/ic_create_white_24dp"
            android:theme="@style/defaultButton" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/ind_1"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark" />

        <TextView
            android:id="@+id/ind_2"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary" />

        <TextView
            android:id="@+id/ind_3"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@color/green_app" />
    </LinearLayout>

</RelativeLayout>

What I need to be able to do that effect?

    
asked by Rosendo Ropher 11.04.2016 в 18:27
source

2 answers

1

In the LinearLayout that contains the textViews use the property:

android:layout_height="?attr/actionBarSize"

With that you assure that the height that takes is the one of the bar:

You can define a fixed measure as another option but I do not think that is the right thing to do:

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentRight="true"
        android:orientation="horizontal">
...
...
...

This would be the corrected Layout of the first option:

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_centerVertical="true">

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />

        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/relativeLayout2"
            android:text="@string/app_name"
            android:textSize="16sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/img_button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="29dp"
        android:src="@drawable/ic_create_white_24dp"
        android:theme="@style/defaultButton" />
</RelativeLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentRight="true"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/ind_1"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark" />

    <TextView
        android:id="@+id/ind_2"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary" />

    <TextView
        android:id="@+id/ind_3"
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:background="@color/green_app" />
</LinearLayout>

    
answered by 11.04.2016 в 21:32
1

To the first RelativeLayout if you set a fixed height as 68dp or else inherit from the high value of a configuration list item ?android:attr/listPreferredItemHeight

<?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="?android:attr/listPreferredItemHeight"
    android:background="@android:color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp"
            android:layout_centerVertical="true">

            <TextView
                android:id="@+id/txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name" />

            <TextView
                android:id="@+id/txt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/relativeLayout2"
                android:text="@string/app_name"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/img_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="29dp"
            android:src="@drawable/ic_create_white_24dp"
            android:theme="@style/defaultButton" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/ind_1"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark" />

        <TextView
            android:id="@+id/ind_2"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary" />

        <TextView
            android:id="@+id/ind_3"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="@color/green_app" />
    </LinearLayout>

</RelativeLayout>
    
answered by 12.04.2016 в 18:29