切换视图的可见性

时间:2016-08-04 12:21:07

标签: android android-fragments relativelayout tablelayout

其中一个布局显示了视图的可见性切换。布局有两个按钮,例如b1和b2。两个按钮显示/展开各自的RelativeLayout。单击按钮时,RelativeLayout的可见性将切换。

在b1 click上展开的RelativeLayout具有edittext实现。 b2在其中扩展RelativeLayout并使用TableLayout。 这就是我试图展示的内容: enter image description here

问题是,有时TableLayout不可见。就像这样: enter image description here

这是onClick实现:

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnSearchByName:
            if (rlSearchByName.getVisibility() == View.VISIBLE) {
                rlSearchByName.setVisibility(View.GONE);
            } else {
                rlSearchByName.setVisibility(View.VISIBLE);
            }
            break;
        case R.id.btnSearchByPartNo:
            if (rlSearchByPartNo.getVisibility() == View.VISIBLE) {
                rlSearchByPartNo.setVisibility(View.GONE);
            } else {
                rlSearchByPartNo.setVisibility(View.VISIBLE);
            }
            break;

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <Button
        android:id="@+id/btnSearchByName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="7dp"
        android:background="@color/transparent"
        android:text="@string/search_by_name"
        android:textColor="@color/orange" />

    <RelativeLayout
        android:id="@+id/rlSearchByName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#30828282"
        android:paddingBottom="5dp"
        android:paddingTop="3dp"
        android:visibility="gone">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_search_by_name"
            android:textColor="@color/grey" />

        <LinearLayout
            android:id="@+id/llNameField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvTitle"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/etLastName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Last Name"
                android:textColorHint="#60828282" />

            <EditText
                android:id="@+id/etFirstName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="First Name"
                android:textColorHint="#60828282" />

            <EditText
                android:id="@+id/etMiddleName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Middle Name"
                android:textColorHint="#60828282" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/llNameField"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnSearch"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:background="@drawable/button_bg"
                android:text="@string/search"
                android:textColor="@color/white" />

            <Button
                android:id="@+id/btnReset"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/button_bg"
                android:text="@string/reset"
                android:textColor="@color/white" />

        </LinearLayout>


    </RelativeLayout>

    <Button
        android:id="@+id/btnSearchByPartNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="7dp"
        android:background="@color/transparent"
        android:text="@string/search_by_part_no"
        android:textColor="@color/orange" />

    <RelativeLayout
        android:id="@+id/rlSearchByPart"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#30828282"
        android:paddingBottom="5dp"
        android:paddingTop="3dp"
        android:visibility="gone">

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <TableRow
                android:id="@+id/trPart12"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true">

                <TextView
                    android:id="@+id/trPart1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_weight="1"
                    android:background="@drawable/border"
                    android:gravity="center"
                    android:text="1"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/trPart2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="2"
                    android:layout_weight="1"
                    android:background="@drawable/border"
                    android:gravity="center"
                    android:text="2"
                    android:textSize="20sp" />
            </TableRow>

            <TableRow
                android:id="@+id/trPart34"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true">

                <TextView
                    android:id="@+id/trPart3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_weight="1"
                    android:background="@drawable/border"
                    android:gravity="center"
                    android:text="3"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/trPart4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_column="1"
                    android:layout_weight="1"
                    android:background="@drawable/border"
                    android:gravity="center"
                    android:text="4"
                    android:textSize="20sp" />
            </TableRow>

        </TableLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/llResultView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="gone">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="10dp"
            android:elevation="3dp"
            android:text="@string/search_results"
            android:textSize="14sp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:scrollbars="vertical" />
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

make two variablr
private boolean first;
private boolean second;

once you click on first buton
then check condition like:


if(first){
//visible
first=false;
}else{
//invisible
first=true;

}