jeudi 28 mai 2015

Android LinearLayout static height converting to wrap_content

I am trying to inflate and display a LinearLayout with a specific height but when displayed the height always gets converted to wrap_content. My layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dialog_title_height"
    android:padding="@dimen/default_padding"
    android:layout_weight="1">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="Scan for devices" />

    <ProgressBar
        android:id="@+id/progressBarScanning"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

It seams fine at first but when I set the ProgressBar visibility to View.GONE then the layout height shrinks to the size of the TextView which is much smaller than the set layout_height on the parent layout.

But, if I wrap the views in a 2nd LinearLayout with the specified height and set the parent to wrap_content then it works just fine. Like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/default_padding"
    android:layout_weight="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dialog_title_height">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="Scan for devices" />

        <ProgressBar
            android:id="@+id/progressBarScanning"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>

But now of course AndroidStudio complains that the 2nd LinearLayout is useless.

What is the proper way to do this?

Aucun commentaire:

Enregistrer un commentaire