【问题标题】:Styling the actionbar in android在 android 中设置操作栏的样式
【发布时间】:2015-05-27 11:22:00
【问题描述】:

我正在关注 [developers.android][1] 网站上的文档来自定义我的操作栏。

问题是操作栏不接受我制作的样式。 这是文件 14/style(我正在 4.3 android 版本设备上测试):

<resources>

    <!-- the theme applied to the application or activity -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/actionbar_color</item>
        <item name="android:icon">@drawable/ic_launcher</item>
    </style>

</resources> 

这是我清单的一部分:

<application

        android:name="com.fakher.actualitefoot.controller.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

我必须做什么?注意:我正在使用支持库,应用程序支持 api-9 [1]:https://developer.android.com/training/basics/actionbar/styling.html#AndroidThemes

【问题讨论】:

  • Toolbar,不要相信android devs上的文档
  • @Florin T 它正在生成此错误:
    > 原因:java.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
  • 您的活动是否扩展了 ActionBarActivity ?尝试将 android:theme="@style/AppTheme" 放在每个活动上。 //忽略我之前的回答。
  • 不,它扩展了“AppCompatActivity”
  • 如果你仔细观察 &lt;item name="android:background"&gt;@drawable/actionbar_background&lt;/item&gt; 背景应该是可绘制的而不是颜色!

标签: android android-actionbar android-appcompat android-styles


【解决方案1】:

我设计了如下所示的操作栏: 我创建了一个 custom_action_bar.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#526e83"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@+id/ContactImageId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:paddingLeft="100dp"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <TextView
            android:id="@+id/CTS"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="CTS"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/UserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:textColor="#ffffff"
            android:text="Welcome: HKH"/>

    </LinearLayout>

</LinearLayout>

然后在我的活动中的 onCreate 中,我调用了这个方法:

public void customActionBar() {

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
    actionBar.setIcon(android.R.color.transparent);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#526e83")));
    View cView = getLayoutInflater().inflate(R.layout.custom_action_bar, null);
    actionBar.setCustomView(cView);
}

我希望这可以帮助你找到你想要的东西

【讨论】:

  • 我的活动应该从哪个类扩展??
  • 你的活动应该extends ActionBarActivity
  • 谢谢它的工作,但它有很多代码!我只想使用默认操作栏并进行一些修改!
  • 你能展示任何你想要的设计或例子吗?我会尽量减少上面的代码以满足你的需要......
  • 谢谢,我按照自己的意愿定制了我的操作栏。但我只是问为什么我不通过styles.xml文件来修改它
【解决方案2】:

如果您想为您的 actionBar 设置样式,还有另一种简单的方法。

 ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8B58A3")));

    LayoutInflater inflater = LayoutInflater.from(this);
    @SuppressLint("InflateParams") View v = inflater.inflate(R.layout.titleview, null);

    TextView frag1 = (TextView)v.findViewById(R.id.titleFragment1);
    frag1.setTypeface(yourdesiredFont);

    actionBar.setCustomView(v);

在你的文本视图中添加你想添加到 actionBar 的任何样式或项目,它会很好用。

【讨论】:

  • 我的活动应该从哪个类扩展??
  • @SaHa 喜欢这个“公共类 MainActivity 扩展 ActionBarActivity”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
相关资源
最近更新 更多