【问题标题】:Why is android activity action bar title black?为什么android活动操作栏标题是黑色的?
【发布时间】:2018-02-18 09:45:40
【问题描述】:

我之前用Android Studio做项目的时候,activity action bar title默认是白色的。 我创建了一个新项目,其设置与之前的项目(API 级别 11)相同,但活动标题颜色的默认颜色为黑色。我根本没有改变任何东西,这是一个新项目。 我尝试使用样式,但没有任何效果。

【问题讨论】:

    标签: android android-activity styles android-api-levels


    【解决方案1】:

    新项目的操作栏标题颜色取决于使用的默认主题。如果操作栏为黑色,标题颜色为白色,请检查清单中使用的主题。

    【讨论】:

    • 它使用默认主题,就是这个。 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources> \n 很抱歉代码混乱。不知道如何在本站格式化。
    • 尝试将“Theme.AppCompat.Light”替换为“Theme.AppCompat.Light.DarkActionBar”,当您的操作栏主题变暗时,标题会自动变亮,并且在此处添加任何代码只需给出四个空格在行之前它将被格式化
    【解决方案2】:

    如果您想以编程方式更改它,下面是代码。

    ActionBar ID 不能直接使用,所以你必须在这里做一点黑客攻击。

     int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    if (actionBarTitleId > 0) {
        TextView title = (TextView) findViewById(actionBarTitleId);
        if (title != null) {
            title.setTextColor(Color.RED);
        }
    }
    

    【讨论】:

      【解决方案3】:

      为了改变操作栏标题的颜色,你可以在你的类中使用这个代码:

      actionBar.setTitle(Html.fromHtml("<font color='#ffffff'>ActionBarTitle </font>"));
      // to choose white 
      

      或在 res/values/styles.xml 文件中更改您的操作栏样式:

      <resources>
          <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
              <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
          </style>
      
          <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
              <item name="android:background">ANY_HEX_COLOR_CODE</item>
              <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
              <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
          </style>
      </resources>
      

      请记住,您应该更改之前在 manifest.xml 中为您的应用选择的样式

      【讨论】:

      • MyTheme 在哪里找到?它应该已经存在了吗?我遇到了错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多