我正在尝试根据复选框首选项显示相对布局。现在,无论复选框处于什么状态,布局都消失了。
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>
最佳答案
您的代码有缺陷 - 无论复选框状态如何,unreadCount.setVisibility(View.GONE);
行都永远不会运行。如果选中该复选框,则将运行 if 语句的开始部分。如果没有检查,第二个嵌套的 if 将跳过该行。您需要取出第二个嵌套的 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);
}
除了此代码 fragment 之外,您的布局中可能存在导致 View 无法显示的缺陷。尝试在任何条件语句之外使用 setVisibility(View.VISIBLE)
行进行测试,看看它是否显示,然后我会检查您的布局或将其发布在此处。
关于java - 为什么我的 setVisibility View 无法正确显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183257/