Android TextView does not expand to match parent

点点圈 提交于 2019-12-22 11:23:20

问题


I have a viewgroup like this image:

I want to not show those texts that are empty. For example, assume that I have no tips and promo then just Notes should be visible. When I test my program Notes (displayed with red background) does not expand to fill parent although its width set to match parent.

Any idea would be appreciated. Thanks.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llNotesContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tracking_bg_note"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/llNotes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tracking_notes"
            android:textColor="@color/tracking_font_address"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/tvNotes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:ellipsize="end"
            android:textColor="@color/tracking_font_note"
            android:textSize="15sp"
            android:background="@color/red"/>
    </LinearLayout>

    <View
        android:id="@+id/vVerticalSeparator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/tracking_separator"/>

    <!-- Tips and Promos -->
    <LinearLayout
        android:id="@+id/llTipsPromos"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <LinearLayout
            android:id="@+id/llTips"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:padding="8dp">

            <TextView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="@string/tracking_tips"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/tvTips"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"
                android:singleLine="true"
                android:ellipsize="end"/>
        </LinearLayout>

        <View
            android:id="@+id/vHorizontalSeparator"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/tracking_separator"/>

        <LinearLayout
            android:id="@+id/llPromos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:padding="8dp">

            <TextView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="@string/tracking_promo"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/tvPromos"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"
                android:singleLine="true"
                android:ellipsize="end"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

In the code I have a method that call once I want to update UI. The method is:

private void showHideSeparators()
    {
        boolean notes = false;
        boolean tips = false;
        boolean promos = false;

        if (!TextUtils.isEmpty(this.tvNotes.getText()))
        {
            notes = true;
        }

        if (!TextUtils.isEmpty(this.tvTips.getText()))
        {
            tips = true;
        }

        if (!TextUtils.isEmpty(this.tvPromos.getText()))
        {
            promos = true;
        }

        // We need to consider 8 configurations due to 3 variables
        if (!notes && !tips && !promos)
        {
            this.llNotesContainer.setVisibility(View.GONE);
            return;
        }

        if (!notes && !tips && promos)
        {
            this.llNotesToDriver.setVisibility(View.GONE);
            this.llTips.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        if (!notes && tips && !promos)
        {
            this.llNotesToDriver.setVisibility(View.GONE);
            this.llPromos.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        if (!notes && tips && promos)
        {
            this.llNotesToDriver.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            return;
        }

        if (notes && !tips && !promos)
        {
            this.llTips.setVisibility(View.GONE);
            this.llPromos.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        if (notes && !tips && promos)
        {
            this.llTips.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            return;
        }

        if (notes && tips && !promos)
        {
            this.llPromos.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        // this is default situation when all is true
        if (notes && tips && promos)
        {
            return;
        }
    }

I even tried to invalidate my textView but nothing has changed. Result is like this when I just have notes.


回答1:


Your android:padding="8dp" is making this.

If you want to have padding just on top and bottom, use

android:paddingBottom="8dp"
android:paddingTop="8dp"



回答2:


If I understand correctly you want to remove the tips and promos field entirely when there are no tips and promos, and display the red notes section to match parent.

In which case make the LinearLayout llTipsPromos gone. This will fill the parent with the red notes section




回答3:


Try below code -

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llNotesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tracking_bg_note">

<LinearLayout
    android:id="@+id/llNotes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/llTipsPromos"
    android:layout_alignParentLeft="true"
    android:orientation="vertical"
    android:padding="8dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tracking_notes"
        android:textColor="@color/tracking_font_address"
        android:textSize="13sp"/>

    <TextView
        android:id="@+id/tvNotes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:ellipsize="end"
        android:textColor="@color/tracking_font_note"
        android:textSize="15sp"
        android:background="@color/red"/>
</LinearLayout>

 <View
    android:id="@+id/vVerticalSeparator"
    android:layout_toRightOf="@+id/llNotes"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:background="@color/tracking_separator"/>

Tips and Promos
<LinearLayout
    android:id="@+id/llTipsPromos"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/llTips"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="visible"
        android:gravity="center_vertical"
        android:padding="8dp">

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="@string/tracking_tips"
            android:textColor="@color/tracking_font_address"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/tvTips"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/tracking_font_note"
            android:textSize="15sp"
            android:singleLine="true"
            android:ellipsize="end"/>
    </LinearLayout>

    <View
        android:id="@+id/vHorizontalSeparator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/tracking_separator"/>

    <LinearLayout
        android:id="@+id/llPromos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:padding="8dp">

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="@string/tracking_promo"
            android:textColor="@color/tracking_font_address"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/tvPromos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/tracking_font_note"
            android:textSize="15sp"
            android:singleLine="true"
            android:ellipsize="end"/>
    </LinearLayout>
</LinearLayout> 

when you want to show note view on full screen then hide your llTipsPromos LinearLayout.It will sure work for you.




回答4:


Finally I found a way to cope with the issue. This is the change. Hope helps you in case you have similar issue.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llNotesContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tracking_bg_note"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/llNotes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tracking_notes"
            android:textColor="@color/tracking_font_address"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/tvNotes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/tracking_font_note"
            android:textSize="15sp"
            android:background="@color/red"/>
    </LinearLayout>

    <View
        android:id="@+id/vVerticalSeparator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/tracking_separator"/>

    <!-- Tips and Promos -->
    <LinearLayout
        android:id="@+id/llTipsPromos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <LinearLayout
            android:id="@+id/llTips"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:padding="8dp">

            <TextView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="@string/tracking_tips"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/tvTips"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"/>
        </LinearLayout>

        <View
            android:id="@+id/vHorizontalSeparator"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/tracking_separator"/>

        <LinearLayout
            android:id="@+id/llPromos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:padding="8dp">

            <TextView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="@string/tracking_promo"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/tvPromos"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


来源:https://stackoverflow.com/questions/29603055/android-textview-does-not-expand-to-match-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!