简体   繁体   English

为什么我的setVisibility View无法正确显示?

[英]Why won't my setVisibility View display correctly?

I'm trying display a Relative Layout based on a checkbox preference. 我正在尝试根据复选框首选项显示相对布局。 Right now, no matter what state the checkbox is in, the layout is gone. 现在,无论复选框处于什么状态,布局都消失了。

final RelativeLayout unreadCount = (RelativeLayout) findViewById(R.id.header_navigation);
            if(_preferences.getBoolean(UNREAD_COUNT_ENABLED_KEY, false)){
                unreadCount.setVisibility(View.VISIBLE);
            }else{
                if(_preferences.getBoolean(UNREAD_COUNT_ENABLED_KEY, true)){
                unreadCount.setVisibility(View.GONE);
            }
        }

<RelativeLayout
    android:id="@+id/header_navigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:gravity="center"
    android:padding="2dp">
    <Button
        android:id="@+id/previous_button" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hapticFeedbackEnabled="true"
        android:background="@drawable/btn_prev_arrow" />
    <TextView
        android:id="@+id/notification_count_text_view"
        android:layout_height="50dip"  
        android:layout_width="wrap_content" 
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/previous_button" 
        android:layout_toLeftOf="@+id/next_button"
        android:gravity="center"
        android:focusable="true" 
        android:clickable="true"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textColor="@color/white"
        android:text="0/0"/>
    <Button
        android:id="@+id/next_button" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:hapticFeedbackEnabled="true"
        android:background="@drawable/btn_next_arrow"/>
</RelativeLayout>

Your code is flawed - no matter what the checkbox state is, the unreadCount.setVisibility(View.GONE); 您的代码存在缺陷 - 无论复选框状态如何,都是unreadCount.setVisibility(View.GONE); line will never be run. 线将永远不会运行。 If the checkbox is checked, the beginning section of the if statement will be run. 如果选中该复选框,则将运行if语句的开始部分。 If it is not checked, the second nested if will skip over that line. 如果未选中,则第二个嵌套if将跳过该行。 You need to take out the second nested if: 如果出现以下情况,您需要取出第二个嵌套:

final RelativeLayout unreadCount = (RelativeLayout) findViewById(R.id.header_navigation);

if(_preferences.getBoolean(UNREAD_COUNT_ENABLED_KEY, false)){
   unreadCount.setVisibility(View.VISIBLE);
}else{
   unreadCount.setVisibility(View.GONE);
}

Aside from this snippet, there is probably a flaw in your layout that is causing the view not to be shown. 除了这个片段之外,您的布局中可能存在一个缺陷,导致视图无法显示。 Try testing it with a setVisibility(View.VISIBLE) line outside of any conditional statements to see if it shows, then I'd check your layout or post it here. 尝试使用任何条件语句之外的setVisibility(View.VISIBLE)行来测试它是否显示,然后我会检查您的布局或在此处发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM