android学习之控件的显示和隐藏
2016-07-20 10:26
477 查看
在android开发过程中,我们有时会需要控制控件的显示和隐藏,来达到一定的ui效果。
而android的View类为我们提供了这样的属性和方法,以是的我们可以控制控件的显示和隐藏;
比如这段代码:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button3" />
</LinearLayout>
在水平布局中有3个按钮,权重为1,3个按钮将水平方向上的空间进行了平分。
我是是可以通过, android:visibility=""来控制控件的显示、隐藏、消失的
android:visibility有3个值invisible(隐藏)、visible(显示)、gone(消失);
默认值为visible,invisible是控制控件不可见,但仍旧占据布局空间,gone是控件不仅是不可见且不再占据布局空间。
Visible:
Invisible:
Gone:
我们不仅可以通过在布局文件中设置android:visibility的值来控制控件的显示和隐藏,我们同样也可在代码中控制,这时我们需要调用到方法:setVisibility();在方法中我们来制定控件的显示状态值,View.GONE/ View.VISIBLE/ View.INVISIBLE
而android的View类为我们提供了这样的属性和方法,以是的我们可以控制控件的显示和隐藏;
比如这段代码:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button3" />
</LinearLayout>
在水平布局中有3个按钮,权重为1,3个按钮将水平方向上的空间进行了平分。
我是是可以通过, android:visibility=""来控制控件的显示、隐藏、消失的
android:visibility有3个值invisible(隐藏)、visible(显示)、gone(消失);
默认值为visible,invisible是控制控件不可见,但仍旧占据布局空间,gone是控件不仅是不可见且不再占据布局空间。
Visible:
Invisible:
Gone:
我们不仅可以通过在布局文件中设置android:visibility的值来控制控件的显示和隐藏,我们同样也可在代码中控制,这时我们需要调用到方法:setVisibility();在方法中我们来制定控件的显示状态值,View.GONE/ View.VISIBLE/ View.INVISIBLE
相关文章推荐
- Android去掉标题栏的两种方法
- Android URI&&URL的定义与使用
- Android中进程间通信(IPC)方式总结
- Android 缓存的编写应用
- Android Volley框架使用(三)
- Cannot launch AVD in emulator
- Android内存泄漏的八种可能
- android之自定义View和ViewGroup(五)(代码篇,实现类似竖着的ViewPager引导页,竖向引导页)
- Android ListView的字母排序和过滤搜索功能
- Android时间控件TimePicker使用
- 自定义View(三)(Android群英传)
- Android Volley框架使用(二)
- Button实现按压变换特效
- android多线程
- Android RecyclerView 使用完全解
- Android 推送到底哪家强
- Android Mac下安装Apk到模拟器上
- Android Camera开发手电筒功能
- Android实现推送方式解决方案
- Android基础之Handler机制学习