我正在尝试自定义操作栏的外观并且它似乎不起作用。当我专门将其设置为红色时,背景显示为灰色。
my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.eliddell.rapsheet"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
<style name="CustomActionBarTheme"
parent="@style/AppTheme">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/CustomActionBarTheme">
<item name="android:background">@color/red</item>
</style>
</resources>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eliddell.rapsheet" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
<activity
android:name=".MainActivity"
android:theme="@style/CustomActionBarTheme"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
如果我将我的样式更新为:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
<style name="CustomActionBarTheme"
parent="@style/AppTheme">
<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/red</item>
</style>
</resources>
然后我简要地看到颜色变化,但应用程序崩溃了:
引起:java.lang.IllegalStateException:你需要使用一个 具有此活动的Theme.AppCompat主题(或后代)。
答案 0 :(得分:12)
好的,所以我想出来了,谢谢大家的帮助。问题是我的样式xml指向&#34; android:background&#34;和&#34; android:actionBarStyle&#34;但随着支持库的到位,&#34; android:&#34;被放弃..
<resources>
<!-- Base application theme. -->
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:windowBackground">@color/sapient_heat</item>
<item name="background">@color/red</item>
</style>
答案 1 :(得分:0)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 11) {
requestWindowFeature(Window.FEATURE_ACTION_BAR);
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 11) {
ActionBar actionBar = this.getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#ff0000")));
actionBar.show();
}
}
在SDK版本11之后使用ActionBar,获取ActionBar的实例而不是你想要的
答案 2 :(得分:0)
在android清单中使用主题:
android:theme="@style/AppTheme"
并且在活动中将其扩展为
public class MainActivity extends ActionBarActivity{
将操作栏的自定义视图扩展为:
// Inflate your custom layout
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.actionbar_layout,
null);
//Set Custom ActionBar
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
其中actionbar_layout是操作栏的布局xml