开发者

TextView takes up space with no text

开发者 https://www.devze.com 2023-04-12 17:37 出处:网络
I have a TextView that is still taking up space when there is no text to display. This happens only on OS 1.6.

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"开发者_StackOverflow >
  <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>


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消