启用和禁用textviews

时间:2018-04-26 15:36:58

标签: android android-relativelayout

具有一些视图的相对布局(textviews,edittext和button)。查看和隐藏textview时,布局不会向上移动。在隐藏视图(textview.setVisibility(View.INVISIBLE))时,它会创建空白区域而不是向上推动视图。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text1"
                android:padding="20dp"/>

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text2"
                android:layout_below="@id/text1"
                android:padding="20dp"/>

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text3"
                android:layout_below="@id/text2"
                android:padding="20dp"/>

            <TextView
                android:id="@+id/text4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text4"
                android:layout_below="@id/text3"
                android:padding="20dp"/>

            <TextView
                android:id="@+id/text5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text5"
                android:layout_below="@id/text4"
                android:visibility="gone"
                android:padding="20dp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/layout1">

            <EditText
                android:id="@+id/edittext1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"/>

            <EditText
                android:id="@+id/edittext2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/edittext1"
                android:padding="20dp"/>

            <EditText
                android:id="@+id/edittext3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/edittext2"
                android:padding="20dp"/>

            <EditText
                android:id="@+id/edittext4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/edittext3"
                android:padding="20dp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/layout2">

            <Button
                android:id="@+id/enable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:layout_margin="10dp"
                android:text="Enable"/>

            <Button
                android:id="@+id/disable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:layout_below="@id/enable"
                android:layout_margin="10dp"
                android:text="disable"/>



        </RelativeLayout>

    </RelativeLayout>

</ScrollView>

活动文件

public class SampleTransparent extends AppCompatActivity {

Button btn_enable, btn_disable;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout3);

    btn_enable = findViewById(R.id.enable);
    btn_disable = findViewById(R.id.disable);
    textView = findViewById(R.id.text5);

    btn_enable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView.setVisibility(View.VISIBLE);
        }
    });

    btn_disable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView.setVisibility(View.INVISIBLE);
        }
    });
}

在启用视图之前

Before Enabling View

启用和禁用视图后

After enabling and disabling the view

2 个答案:

答案 0 :(得分:0)

将任何视图的可见性设置为View.INVISIBLE只会隐藏视图。视图未显示但占用了它所需的空间。

如果您不希望它也占用任何空间,则应将可见性设置为View.GONE

官方文件说明如下:

View.INVISIBLE

  

此视图不可见,但仍占用布局空间   目的。与setVisibility(int)和android:visibility。一起使用。

View.GONE

  

此视图不可见,并且不占用任何布局空间   目的。与setVisibility(int)和android:visibility。一起使用。

答案 1 :(得分:0)

View.GONE此视图不可见,并且不会占用任何空间进行布局。

View.INVISIBLE此视图不可见,但仍会占用空间以进行布局。

btn_disable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView.setVisibility(View.GONE);
        }
    });
相关问题