【问题标题】:Navigation drawer change color导航抽屉更改颜色
【发布时间】:2013-09-12 08:57:23
【问题描述】:

我正在使用this 示例使用导航抽屉。我只想将蓝色更改为橙​​色,我该怎么做?我的意思是改变列表视图选择器颜色,操作栏选择器颜色一切。

【问题讨论】:

    标签: android android-layout navigation-drawer


    【解决方案1】:

    你可以使用选择器

    在drawable文件夹中有selector.xml如下

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

    在drawable文件夹中按.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <solid android:color="#FF9D21"/>     
    </shape>
    

    在可绘制文件夹normal.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <solid android:color="#8BC7EB"/>    
    </shape>
    

    在drawer_list_item.xml中

    android:background="@drawable/selector"
    

    用于设置栏的样式

      <resources>
    
        <!--
            Base application theme for API 11+. This theme completely replaces
            AppBaseTheme from res/values/styles.xml on API 11+ devices.
        -->
        <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
             <item name="android:background">@style/MyActionBar</item>
            <!-- API 11 theme customizations can go here. -->
        </style>
        <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
            <item name="android:background">@drawable/press</item>
    
        </style>
    </resources>
    

    在您的清单中

     <activity
            android:name=".MainActivity"
              android:theme="@style/MyActionBar"
            android:label="@string/app_name">
    

    您可以为特定活动而不是整个应用程序指定主题。

    更多信息

    http://android-developers.blogspot.in/2011/04/customizing-action-bar.html

    https://developer.android.com/training/basics/actionbar/styling.html

    快照

    【讨论】:

    • 也只是自定义listView或者actionbar?
    • @bauer9664 仅用于抽屉列表项。
    • @bauer9664 使用样式更改条形颜色developer.android.com/training/basics/actionbar/styling.html
    • @bauer9664 检查快照。条形颜色变为橙色。从模拟器拍摄的快照看起来不太好
    • 您还应该在选择器中包含android:layout_height=""android:layout_width="" 属性,否则Android Studio/Gradle 将无法编译您的应用
    【解决方案2】:

    你必须为此做两件事。

    1. 为列表视图创建您的自定义列表视图选择器(请参阅答案@raghunandan)
    2. for actionbar Go here 创建您的样式并从清单设置您的样式,将您的应用程序主题设置为您创建的样式

    【讨论】:

    • 这是一个很棒的网站。从来没有听说过这个。谢谢
    【解决方案3】:

    试试这个代码,

    mDrawerLayout.setBackgroundResource(int);
    

    【讨论】:

      【解决方案4】:

      您需要使用选择器。

      查看本教程了解更多信息。 customizing listviews

      【讨论】:

        【解决方案5】:

        对于ActionBar backgroownd:

        <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:actionBarItemBackground">@drawable/bg_actionbar</item>
        </style>
        </resources>
        

        【讨论】:

          【解决方案6】:

          我想为@Raghunandan 给出的答案做出贡献。在列表中,您可以区分单击的项目(android:state_pressed)和激活的项目(android:state_activated),因此,如果您单击列表中已激活的项目,则单击是可视化的。

          在drawable文件夹下创建一个activated.xml:

          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
              <solid android:color="#6A6A6A"/>
          </shape>
          

          并修改 selector.xml 添加 state_activated 修饰符:

          <?xml version="1.0" encoding="utf-8"?>
          <selector xmlns:android="http://schemas.android.com/apk/res/android">
              <item android:state_pressed="true" 
                  android:drawable="@drawable/press" />
          
              <item android:state_activated="true" 
                  android:drawable="@drawable/activated" />
          
              <item android:state_focused="false" 
                  android:drawable="@drawable/normal" />
          </selector>
          

          【讨论】:

            【解决方案7】:

            使用选择器来实现这一点。您可以使用@drawable@color

            item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
            
            item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
            
            item android:drawable="@drawable/list_item_bg_selected" android:state_activated="true"/
            

            【讨论】:

              猜你喜欢
              • 2015-10-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-06-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-12-29
              相关资源
              最近更新 更多