Android TextView 不会扩展以匹配父级 [英] Android TextView does not expand to match parent
问题描述
我有一个这样的视图组:
I have a viewgroup like this image:
我不想显示那些空白的文本.例如,假设我没有提示和促销,那么应该只显示注释.当我测试我的程序时,Notes(以红色背景显示)不会扩展以填充父级,尽管其宽度设置为与父级匹配.
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>
在代码中,我有一个方法,一旦我想更新 UI,就会调用该方法.方法是:
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;
}
}
我什至试图使我的 textView 无效,但没有任何改变.当我只有笔记时,结果是这样的.
I even tried to invalidate my textView but nothing has changed. Result is like this when I just have notes.
推荐答案
我终于找到了解决这个问题的方法.这是变化:
Finally I found a way to cope with the issue. This is the change:
<?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>
这篇关于Android TextView 不会扩展以匹配父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!