【问题标题】:Custom background color for selected item with "activatedBackgroundIndicator" Navigation Drawer使用“activatedBackgroundIndicator”导航抽屉为所选项目自定义背景颜色
【发布时间】:2014-04-16 14:28:10
【问题描述】:

这个问题在 SO 上被问了很多,我参考了所有的答案。我的导航抽屉上的选定项目仍然使用默认的 Holo 蓝色背景。我是 Java 新手,我对 .setAdapter() 的“上下文”部分感到困惑。
我的项目是单个 Activity,其中使用导航抽屉交换了多个片段。

这是我的适配器:

mDrawerListView.setAdapter(new ArrayAdapter<String>(
            // First parameter - Context
            getActionBar().getThemedContext(),
            // Second parameter - Layout for the row
            R.layout.fragment_navigation_drawer_list_item,
            // Third parameter - ID of the TextView to which the data is written
            android.R.id.text1,
            // Forth - the Array of data
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
                    getString(R.string.title_section4),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

这里的上下文来自 Android Studio 中的“预煮”导航抽屉。我认为这将是Navigation Drawer item background colour for selected item 的答案。所以我将上下文更改为getActivity().getBaseContext(),,但这并没有改变任何东西。

我的主题 (styles.xml):

<resources>
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
        <item name="android:actionBarStyle">@style/ActionBar</item>
    </style>

    <!-- Navigation Drawer styling -->
    <style name="NavDrawerItemSelected" parent="AppBaseTheme">
        <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    </style>
</resources>

activated_background 在“drawables”目录中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/green" />
    <item android:state_selected="true" android:drawable="@color/green" />
    <item android:state_pressed="true" android:drawable="@color/green" />
    <item android:state_checked="true" android:drawable="@color/green" />        
    <item android:drawable="@android:color/transparent" />
</selector>

我不知道应该使用上述哪些状态,所以我添加了所有我能找到的状态。
最后,当一个项目被选中时,mDrawerListView.setItemChecked(position, true); 被调用。
一切正常,除了自定义主题样式。 (最小 API = 11,在 API 17 AVD 上测试)

【问题讨论】:

    标签: android android-fragments android-theme


    【解决方案1】:

    我遇到了这个确切的问题。问题是R.layout.fragment_navigation_drawer_list_item 的背景需要更改为您的可绘制对象。只需在布局中添加android:background="@drawable/activated_background"

    【讨论】:

    • 谢谢!我不敢相信我在其他答案中没有遇到过。我以前有android:background=?android:activatedBackgroundIndicator
    【解决方案2】:

    我无法找到这个问题的任何完整明确的答案,所以这里是:

    步骤 1. 指定适配器将使用的列表项布局,在本例中,我们指定:R.layout.fragment_navigation_drawer_list_item

    像这样:

    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            // First parameter - Context
            getActionBar().getThemedContext(),
            // Second parameter - Layout for the row
            R.layout.fragment_navigation_drawer_list_item,
            // Third parameter - ID of the TextView to which the data is written
            android.R.id.text1,
            // Forth - the Array of data
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
                    getString(R.string.title_section4),
            }))`
    

    第 2 步。创建和自定义 fragment_navigation_drawer_list_item.xml 以指定具有如下选择器的可绘制对象:android:background="@drawable/activated_background" 完整示例:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:background="@drawable/activated_background" />
    

    第 3 步。在您的 activated_background.xml 文件中创建和自定义选择器,就像在问题中一样,它看起来像这样:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_activated="true" android:drawable="@color/green" />
        <item android:state_selected="true" android:drawable="@color/green" />
        <item android:state_pressed="true" android:drawable="@color/green" />
        <item android:state_checked="true" android:drawable="@color/green" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 2016-05-15
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多