[英]Android light theme with action bar
I want to use a light theme, but still have an actionbar visible. 我想使用浅色主题,但仍然可以看到操作栏。
From here it looks like this should work: 从这里看起来应该可以正常工作:
<activity android:theme="@style/Theme.AppCompat.Light" ... >
But when I add any android:theme the actionbar disappears. 但是,当我添加任何android:theme时,动作栏就会消失。
I'm probably missing something obvious... 我可能缺少明显的东西...
Edit: in case it helps 编辑:以防万一
From the build.gradle 从build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.company.app"
minSdkVersion 15
targetSdkVersion 21
versionCode 5
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
activity_main.xml: activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/screen_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".owg">
<TextView android:id="@+id/textHeader"
android:textSize="24sp"
android:textColor="#006699"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:visibility="visible"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
android:visibility="invisible"
/>
</LinearLayout>
main.java main.java
package com.company.app;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.content.Intent;
import android.view.View;
public class Main extends ListActivity {
private String[] mBooks = new String[]{ "Informational", "Extensions" };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySimpleArrayAdapter booksAdapter = new MySimpleArrayAdapter(this,mBooks);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(booksAdapter);
}
}
use this: 用这个:
<activity android:theme="@style/Theme.AppCompat.Light.DarkActionBar" ... >
instead of : 代替 :
<activity android:theme="@style/Theme.AppCompat.Light" ... >
It should work. 它应该工作。 The issue here, that you use style without actionBar now, and there is some choices how action bar will look(see related styles) 这里的问题是,您现在使用不带actionBar的样式,并且可以选择一些操作栏的外观(请参阅相关样式)
//Use this to fill your action bar with items
@Override public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu_name, menu);
return true;
}
or if yout want custom view for your actionBar 或者如果您想为actionBar自定义视图
//Getting action bar
mActionBar = getActionBar();
and then use 然后使用
mActionBar.setCustomView(mCustomView);//View object for your actionBar
mActionBar.setDisplayShowCustomEnabled(true)
EDIT: if you can't see title bar you can create it programmatically 编辑:如果看不到标题栏,则可以以编程方式创建它
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.settings);
activity.setTitle("Settings");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.