美文网首页
Button控件的动态显示与隐藏

Button控件的动态显示与隐藏

作者: 沐雨丹阳 | 来源:发表于2018-10-10 15:34 被阅读0次

有时我们会有这样的需求,本来界面有两个控件纵向排列,初始只显示上面的控件,根据某种场景需要显示下面的控件,例如,有个账号列表,在列表里选择一些项想要删除掉时,界面上需要出现可以点击的删除按钮。这个删除按钮最初是不需要显示的。实现的过程如下,做个记录。
1.首先需要一个LinearLayout父控件布局。
2.其次在第一步的父布局的LinearLayout中添加一个列表控件和按钮控件,这里我们就用: RecyclerView和
Button两个控件。
3.设置RecyclerView和Button两个控件的属性,直接上代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right|center_horizontal"
    android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/Recycler_View_account_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:scrollbars="vertical"
        tools:listitem="@android:layout/simple_list_item_1" />
    <Button
        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="删除" />
</LinearLayout>

注意以下这三个属性的设置:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
4.在activity里切换按钮的隐藏或显示:

    if(bHaveSelectedFlag)
        mButtonDelete.setVisibility( View.VISIBLE );
    else {
        mButtonDelete.setVisibility( View.GONE );
    }

相关文章

网友评论

      本文标题:Button控件的动态显示与隐藏

      本文链接:https://www.haomeiwen.com/subject/rcnpaftx.html