android学习之控件的显示和隐藏

来源:互联网 发布:bigworld源码 编辑:程序博客网 时间:2024/06/05 20:54

在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

1 0
原创粉丝点击