当两个小部件一个在另一个之上时,设置小部件可见性

时间:2016-03-04 08:36:25

标签: android android-layout android-fragments

我需要创建一个像下面这样的片段。

点击Need Cash TextView将更改包含的布局,其中包含两个按钮Cancel& Meet并在这些观点之间切换。

但是当我点击需要现金通话时,toggleVisibility()功能会使Need Cash隐身,但不显示Cancel | Meet

我在这个过程中犯了什么错误?

|----------------------|        =>        |----------------------|
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|                      |        =>        |                      |
|----------------------|        =>        |----------------------|
|     Need Cash        |        =>        | Cancel  |   Meet     |
|----------------------|        =>        |----------------------|







@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.tv_need_cash:
                toggleVisibility();
            break;
        case R.id.tv_need_cash_cancel:
            toggleVisibility();
            Toast.makeText(getActivity(), "tv_need_cash_cancel", Toast.LENGTH_SHORT).show();
            break;

        case R.id.tv_need_cash_lets_meet:
            toggleVisibility();
            Toast.makeText(getActivity(), "tv_need_cash_lets_meet", Toast.LENGTH_SHORT).show();
            break;
    }
}

public void toggleVisibility() {
    if(needCashTv.getVisibility() == View.VISIBLE) {
        //needCashTv.setEnabled(false);
        needCashTv.setVisibility(View.GONE);
        frameForNeedCashStuff.bringToFront();

        frameForNeedCashStuff.setVisibility(View.VISIBLE);
        //cancelNeedCashTv.setEnabled(true);
        cancelNeedCashTv.setVisibility(View.VISIBLE);
        //letsMeetneedCashTv.setEnabled(true);
        letsMeetneedCashTv.setVisibility(View.VISIBLE);
    }
    else {
        //needCashTv.setEnabled(true);
        needCashTv.setVisibility(View.VISIBLE);
        needCashTv.bringToFront();

        frameForNeedCashStuff.setVisibility(View.GONE);
        //cancelNeedCashTv.setEnabled(false);
        cancelNeedCashTv.setVisibility(View.GONE);
        //letsMeetneedCashTv.setEnabled(false);
        letsMeetneedCashTv.setVisibility(View.GONE);
    }
    thisView.invalidate();
}


.....................OTHER WIDGETS ABOVE......................
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_alignParentBottom="true"
    android:id="@+id/frameForNeedCashStuff">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:visibility="gone">
        <TextView
            android:layout_width="200dp"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:layout_alignParentLeft="true"
            android:background="@color/ColorPrimary"
            android:gravity="center"
            android:id="@+id/tv_need_cash_cancel"
            android:text="@string/button_cancel"
            android:textSize="@dimen/text_sizes_small"
            android:textColor="@color/White"
            android:visibility="gone"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:layout_alignParentRight="true"

            android:background="@color/ColorPrimary"
            android:gravity="center"
            android:id="@+id/tv_need_cash_lets_meet"
            android:text="Let's Meet"
            android:textSize="@dimen/text_sizes_small"
            android:textColor="@color/White"
            android:visibility="gone"/>
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:background="@color/ColorPrimary"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:id="@+id/tv_need_cash"
        android:text="@string/need_cash"
        android:textSize="@dimen/text_sizes_small"
        android:textColor="@color/White"
        android:visibility="visible" />
</RelativeLayout>
.....................OTHER WIDGETS BELOW......................

修改

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View thisView =  inflater.inflate(R.layout.fragment_map, container, false);
return thisView;
}

编辑2

点击之前的片段视图,显示保持现金TextView

Fragment view before clicking which is showing the Keep Cash TextView

点击后的片段视图未显示&#39;取消&#39;并且&#39;见面&#39; TextViews

Fragment view after clicking which is NOT showing the 'Cancel' and 'Meet' TextViews

**编辑3 ** 给予外部RelativeLayout的id并使其无效

public void toggleVisibility() {
        if(needCashTv.getVisibility() == View.VISIBLE) {
            //needCashTv.setEnabled(false);
            needCashTv.setVisibility(View.GONE);

            frameForNeedCashStuff.setVisibility(View.VISIBLE);
            //cancelNeedCashTv.setEnabled(true);
            cancelNeedCashTv.setVisibility(View.VISIBLE);
            //letsMeetneedCashTv.setEnabled(true);
            letsMeetneedCashTv.setVisibility(View.VISIBLE);
            frameForNeedCashStuff.bringToFront();
        }
        else {
            //needCashTv.setEnabled(true);
            needCashTv.setVisibility(View.VISIBLE);
            needCashTv.bringToFront();

            frameForNeedCashStuff.setVisibility(View.GONE);
            //cancelNeedCashTv.setEnabled(false);
            cancelNeedCashTv.setVisibility(View.GONE);
            //letsMeetneedCashTv.setEnabled(false);
            letsMeetneedCashTv.setVisibility(View.GONE);
        }
        ((ViewGroup)frameForNeedCashStuff2.getParent()).invalidate();
        //thisView.invalidate();
    }

修复后的工作代码 编辑4

public void toggleVisibility() {
    if(needCashTv.getVisibility() == View.VISIBLE) {
        needCashTv.setVisibility(View.GONE);

        frameForNeedCashStuff2.setVisibility(View.VISIBLE);
    }
    else {
        needCashTv.setVisibility(View.VISIBLE);
        needCashTv.bringToFront();

        frameForNeedCashStuff2.setVisibility(View.GONE);
    }
    ((ViewGroup)frameForNeedCashStuff.getParent()).invalidate();
}


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_alignParentBottom="true"
    android:id="@+id/frameForNeedCashStuff">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:visibility="gone"
    android:id="@+id/frameForNeedCashStuff2">
    <TextView
        android:layout_width="200dp"
        android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:layout_alignParentLeft="true"
        android:background="@color/ColorPrimary"
        android:gravity="center"
        android:id="@+id/tv_need_cash_cancel"
        android:text="@string/button_cancel"
        android:textSize="@dimen/text_sizes_small"
        android:textColor="@color/White"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:layout_alignParentRight="true"

        android:background="@color/ColorPrimary"
        android:gravity="center"
        android:id="@+id/tv_need_cash_lets_meet"
        android:text="Let's Meet"
        android:textSize="@dimen/text_sizes_small"
        android:textColor="@color/White"/>
    </RelativeLayout>

    <TextView
    android:layout_width="match_parent"
    android:layout_height="@dimen/abc_action_bar_default_height_material"
    android:background="@color/ColorPrimary"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:id="@+id/tv_need_cash"
    android:text="@string/need_cash"
    android:textSize="@dimen/text_sizes_small"
    android:textColor="@color/White"
    android:visibility="visible" />
</FrameLayout>

1 个答案:

答案 0 :(得分:0)

RelativeLayout TextView包裹tv_need_cash_cancel tv_need_cash_lets_meetView.invalidate()已设置为“已消失”。

如果您只是切换子视图的可见性,它将永远不会显示。

您确定还需要regexp {.*%MKA-5-SESSION_START: +\((.*) +:.*\) +MKA Session started for.*([0-9A-Fa-f]{4}.[0-9A-Fa-f]{4}.[0-9A-Fa-f]{4}).*AuditSessionID +(.*), +AuthMgr-Handle +(.*)} $op - intf mac auditsessid authmgrhdl 吗?如果您只是切换可见性,则没有必要。