TextView 占用空间但没有文本

发布于 2024-12-09 08:09:08 字数 971 浏览 0 评论 0原文

我有一个 TextView,当没有文本可显示时,它仍然占用空间。这种情况仅发生在 OS 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>

I have a TextView that is still taking up space when there is no text to display. This happens only on OS 1.6.

When my app runs on 2.2, the height of the TextView collapses so that it doesn't take up any space.

Is there some property I can set in TextView to because it to collapse or disappear when no text is set? Here is my XML code.

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲜血染红嫁衣 2024-12-16 08:09:08

如果您想让文本视图在文本为 null消失,请使用 android:visibility 或以编程方式使用 textview.setVisibility(View .INVISIBLE);textview.setVisibility(View.GONE);

If you want to make a textview disappear if its text is null then use android:visibility or programatically use textview.setVisibility(View.INVISIBLE); or textview.setVisibility(View.GONE);.

谁的年少不轻狂 2024-12-16 08:09:08

对于你所要求的,你必须使用 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);
            }
        }
    });

并且任何时候您想要更改其中的文本,只需在设置之前使用 your_text_view.setVisibility(View.VISIBLE);新文本至它,如果新文本为空,它将不会显示,因为它会在您注意到之前消失,并且在它已经可见的情况下,此行不会造成任何伤害或影响。

只需确保将 TextView 声明为通用变量,以便能够在 TextWatcher 中使用它。

for what you asking, you have to use TextWatcher, and then when the text is empty just make the TextView disappear:

    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);
            }
        }
    });

and any time you want to change the Text in it, just use your_text_view.setVisibility(View.VISIBLE); just before you set the new text to it, and if the new text will be empty it will not be shown as it will disappear before you even notice, and in the case it is already VISIBLE, this line won't make any harm or affection.

just make sure to declare your TextView as a general variable in order to be able to use it within the TextWatcher.

卷耳 2024-12-16 08:09:08
textview.setVisibility(View.INVISIBLE);
textview.setHeight(0);
textview.setVisibility(View.INVISIBLE);
textview.setHeight(0);
一影成城 2024-12-16 08:09:08

只是为了给出更完整的答案。您可以通过检查每次通过 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 中没有遇到过很多没有可行的更改侦听器的情况,这适用于向视图添加功能的大多数问题。

Just to give a more complete Answer. You can give the TextView AutoCollapse capabilities by checking what text is being displayed whenever it is changed via TextChangedListener

I would simply use

.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) {

            }
        });

and it now has the functionality that you are looking for.
I haven't encountered many things in android UI that don't have a viable change listener, this is applicable for most issues of adding functionality to views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文