【问题标题】:Align the views in relativelayout after enabling and disabling the textviews启用和禁用 textviews 后在 relativelayout 中对齐视图
【发布时间】:2018-09-28 05:42:43
【问题描述】:

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

启用视图之前

启用和禁用视图后

【问题讨论】:

  • 而不是隐形尝试让可见性转到布局

标签: android android-relativelayout


【解决方案1】:

View.GONE这个视图是不可见的,它不占用任何空间来布局。

View.INVISIBLE这个视图是不可见的,但它仍然占用空间用于布局。

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

【讨论】:

    【解决方案2】:

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

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

    官方文档声明如下:

    View.INVISIBLE

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

    View.GONE

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

    【讨论】:

      猜你喜欢
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多