android - 启用和禁用 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);
}
});
}
启用视图之前
启用和禁用视图后
解决方案
将任何视图的可见性设置为View.INVISIBLE
仅隐藏视图。该视图未显示,但它占用了它所需的空间。
View.GONE
如果您不希望它也占用任何空间,则应将可见性设置为。
官方文档声明如下:
这个视图是不可见的,但它仍然占用空间用于布局。与 setVisibility(int) 和 android:visibility 一起使用。
此视图是不可见的,并且它不占用任何空间用于布局目的。与 setVisibility(int) 和 android:visibility 一起使用。
推荐阅读
- javafx - Raspbian Stretch Javafx“无法打开显示”
- jpa - JPA 事务未启动...如何解决?
- android - 仅允许 Android 访问 Firestore 数据库
- html - Angular - 无法连接 HTML 页面中的值
- android - 调用 onPause 后重新启动活动
- javascript - 如何避免 jquery ajax 错误消息打印到控制台
- kotlin - 从 java 类中检索 kotlin 注释
- jquery - 有没有办法在下拉列表中获取非选定项目的值?
- c - 按位移位 (varUint8 >> 7) & 0x01u - Misra 兼容
- cmake - 将 CMakeLists.txt 的生成集成到所述 CMakeLists.txt