【问题标题】:Android 4.4: ToggleButton background is visible even the view is "GONE"Android 4.4:即使视图为“GONE”,也可以看到 ToggleButton 背景
【发布时间】:2015-05-18 00:51:25
【问题描述】:

看来我的问题特定于 Android 4.4 及更高版本。这是我想要做的:

我有一个带有 GroupViews 和 ChapterViews 的 ExpandableListView。 GroupViews 包含一个切换按钮,仅当它们具有子视图时才可见,否则设置为 View.GONE(这也是默认值)

 ToggleButton toggleExpansion = (ToggleButton) chapter.findViewById(R.id.toggleExpansion);
        if(getChildrenCount(groupPosition) == 0){ // Not an expandable group
            text.setTextColor(getResources().getColor(R.color.title_textcolor_empty));
            toggleExpansion.setVisibility(View.GONE);
        }else{  // An expandable group

            toggleExpansion.setVisibility(View.VISIBLE);

            if(isExpanded){
                toggleExpansion.setChecked(true);
            }else{
                toggleExpansion.setChecked(false);
            }
        }

这些按钮的背景由选择器定义:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/maximized_group_item" />
    <item android:state_checked="false" android:drawable="@drawable/minimized_group_item" />
</selector>

这里是上面夸大的 GroupView 的布局定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="horizontal">
    <ToggleButton
        android:id="@+id/toggleExpansion"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_margin="2dp"
        android:background="@drawable/list_expander"
        android:padding="4dp"
        android:clickable="false"
        android:focusable="false"
        android:textOff=" "
        android:textOn=" "
        android:visibility="gone" />

    <TextView
        android:id="@+id/textChapterTitle"
        style="@style/list_text"
        android:textStyle="bold" />

</LinearLayout>

当我运行代码时,它在 Android 4.1.2 中运行良好且符合预期,但在 4.4(在模拟器和设备上)中,即使 ToggleButton 为“GONE”,图像也会显示 minimized_group_item。

所以我认为这与Android版本有关。在我将设备从 2.3.3 更新到 CM11 (4.4) 之前,这段代码运行良好。

我在网上找不到任何相关内容。我还准备了屏幕截图,但无法发布(声誉 here。 我希望你觉得这种奇怪的行为很有趣,我想知道如何解决这个问题。

【问题讨论】:

  • 样式和主题可能会出现与 Android 版本相关的布局差异。您是否尝试过注释掉 TextViews 样式?另外,您是否认为视图消失了,但实际上它是不可见的,因为它的颜色?喜欢黑色背景上的黑色文字?
  • TextViews 样式只有宽度、高度、边距等属性。灰色背景显示,TextView 所在的位置。我尝试过使用模拟器和设备。我不认为主题应该是任何问题。视图消失了,我知道这是因为视图不占用布局上的任何位置。我也尝试过 INVISIBLE。无论如何,即使视图是可见的,也会以某种方式出现第二个(错误的)图标。
  • 第一个图标顶部的第二个图标可能是过度绘制的结果。尝试在其适配器上使用 notifyDataSetChanged 使 ListView 无效以强制完全重绘。
  • 感谢您的帮助。我在下面发布了一个解决方案。这纯粹是我的错误。但是把它放在那里很有用,setGroupIndicator 在不同版本的 Android 中表现不同。

标签: android android-layout android-selector android-togglebutton


【解决方案1】:

这个项目很老了,几个月来我什么都没做。显然我在代码中设置了expandableLectures.setGroupIndicator(getResources().getDrawable(R.drawable.list_expander));

选择器作为组指示器不起作用。它甚至没有出现在 Android 4.4 以下。因此我认为这是由于Android版本的原因,但只是因为我将其设置为GroupIndicator。

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多