应用程序小组件:将背景添加到外部布局

时间:2016-03-08 19:55:40

标签: java android android-layout android-appwidget

这件事让我抓狂。这是它的工作原理

1。一切都设为默认

  • 涟漪效应
  • 列表视图项分隔符可见

original state

2。白色背景添加到小部件布局

  • 涟漪丢失
  • 列表视图项目分隔符也已消失
  • 看起来已删除列表项样式

white background

这是代码

主要小部件布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              ...
              android:background="@android:color/white" -- this line only applies for case 2
              android:padding="@dimen/widget_padding">

    <TextView
        ...
        android:background="@color/primary"
        android:textColor="@android:color/white"/>

    <ListView
        ...
        android:drawSelectorOnTop="true""/>

    <TextView
        ...
        android:textColor="?android:attr/textColorSecondary"/>

</LinearLayout>

列出项目布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:id="@+id/widgetItem">

    <TextView
        ...
        android:textColor="@android:color/black"
        android:textSize="14sp"/>

    <TextView
        ...
        android:textColor="@color/negative_amount"
        android:textSize="16sp"
        android:textStyle="bold"/>

</RelativeLayout>

我花了一天时间尝试所有可能的组合,但没有任何帮助。并且我没有得到这样的事实:列表视图周围的某些布局的无关背景更改完全改变了行为。 WTF?

我想以最干净的方式解决它 - e.i.没有自定义选择器的黑客攻击。如果可能的话,这应该可以直接使用。

1 个答案:

答案 0 :(得分:1)

看起来你正在为你的活动使用黑暗主题,因此纹波是白色的,因此在白色背景下看不到。最简单的解决方案是使用主题的轻微变体,这将导致纹波变黑。

例如,如果您使用的是AppCompat主题,则可以将此行添加到ListView

<ListView
    ...
    android:drawSelectorOnTop="true"
    style="@style/Theme.AppCompat.Light"/>

您也可以将其应用于视图层次结构,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      ...
      android:background="@android:color/white"
      android:padding="@dimen/widget_padding"
      android:theme="@style/Theme.AppCompat.Light">

这将导致主题在LinearLayout及其所有子视图中使用。

此外,您可以通过将android:theme属性添加到<activity><application>标记到AndroidManifest.xml来指定主题活动范围甚至整个应用范围。