TextView占用空间但没有文本

8
我有一个TextView,在没有显示文本时仍然占据空间。这只会在1.6操作系统上发生。
当我的应用程序在2.2上运行时,TextView的高度会崩溃,以便不占用任何空间。
是否有一些属性我可以在TextView中设置,使其在未设置文本时折叠或消失?以下是我的XML代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:layout_marginTop="3dp"
  android:layout_marginRight="3dp" 
  android:background="#ff737373"
  android:padding="3dp"
  android:minWidth="64dp" >
  <ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:tag="tabImage"></ImageView>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:tag="tabCaption"
    android:textColor="#ffd9d9d9" />
</LinearLayout>
4个回答

7

如果你想让一个textview在它的文本为空时“消失”,那么可以使用android:visibility或编程方式使用textview.setVisibility(View.INVISIBLE);textview.setVisibility(View.GONE);


但这会使它始终不可见或消失。我希望只有在TextView没有设置任何文本时,TextView才会消失。 - Johann
那么,你不能测试一下TextView是否有文本吗? - Paresh Mayani
4
但是View.INVISIBLEView.GONE有很大的区别。 - Kartik Domadiya
我只是提出了这些功能,可以根据他的要求进行实现。还是让我澄清一下:Invisible 意味着视图位置存在,但视图不存在。而在 GONE 中,两者都不存在。 - Paresh Mayani
1
我正在寻找一些可以在XML中设置的东西,当它检测到没有文本可用时,自动删除或隐藏TextView。正如我所指出的那样,在OS 2.2中会自动发生这种情况,但在1.6中不会,因此谷歌改变了这种行为。无论如何,我决定在没有文本可用时通过编程方式移除TextView。 - Johann
每次更改TextView的文本时,只需检查文本长度。如果为null/length = 0,则将可见性设置为GONE,否则将可见性设置为VISIBLE。 - banzai86

2
你需要使用TextWatcher来实现你的需求,当文本为空时,将TextView隐藏即可。
    your_text_view.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(your_text_view.getText().toString().equals("")){
                your_text_view.setVisibility(View.GONE);
            }
        }
    });

如果您想更改 TextView 中的文本,只需在设置新文本之前使用 your_text_view.setVisibility(View.VISIBLE);。如果新文本为空,则不会显示,因为它将在您注意到之前消失。如果已经可见,这行代码不会产生任何影响。

请确保将 TextView 声明为一般变量,以便能够在 TextWatcher 中使用它。


0

为了给出更完整的答案,您可以通过检查TextChangedListener在每次更改时显示的文本来为TextView提供AutoCollapse功能。

我会简单地使用

.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.length()>0)
                    textView.setVisibility(View.VISIBLE);
                else textView.setVisibility(View.INVISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

现在它已经具备了您所需要的功能。我还没有遇到过在Android UI中没有适当变更监听器的情况,这对于大多数视图添加功能的问题都是适用的。


0
textview.setVisibility(View.INVISIBLE);
textview.setHeight(0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接