我正在关注developers.android网站上的文档以自定义我的操作栏。
问题在于操作栏不接受我制作的样式。 这是文件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
答案 0 :(得分:1)
我设计了我的Action Bar,如下所示: 我创建了 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);
}
我希望这可以帮助你找到你想要的东西
答案 1 :(得分:0)
如果你想设置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);
并在textview中添加要添加到actionBar的任何样式或项目,它会很有用。