首页 > 解决方案 > 启用和禁用 textviews 后在 relativelayout 中对齐视图

问题描述

有一些视图(文本视图、编辑文本和按钮)的相对布局。查看和隐藏 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);
        }
    });
}

启用视图之前

启用视图之前

启用和禁用视图后

启用和禁用视图后

标签: androidandroid-relativelayout

解决方案


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

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

官方文档声明如下:

View.INVISIBLE

这个视图是不可见的,但它仍然占用空间用于布局。与 setVisibility(int) 和 android:visibility 一起使用。

查看.GONE

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


推荐阅读