繁体   English   中英

无法在android studio中更改主题

[英]unable to change theme in android studio

尝试使用溢出菜单中的三个按钮制作一个简单的溢出应用程序,每个按钮表示activity_main.xml的三种不同背景色,但是每次我尝试更改应用程序的主题时。启动时,应用程序崩溃模拟器显示不幸地停止了应用程序

我想制作应用程序Theme.Holo或任何显示溢出菜单按钮的主题

该应用程序只能在以下代码中进行以下修改:

manifest.xml文件

<application
android:theme="@style/AppTheme"
... >
</application>

style.xml文件

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

在这种情况下,应用程序窗口中的“溢出”按钮不可见

上面两个代码中的任何更改(例如,问题末尾给出的代码显示错误)都会使该应用显示错误:打开该应用时,不幸的是流量溢出

在Android上更改Theme.Holo中讨论了类似的问题Manifest.xml文件未在Android上运行

我也阅读https://developer.android.com/training/basics/actionbar/styling.html

但我不明白解决方案...

所有应用程序文件都复制在下面。由于程序清单和样式文件被编辑,因此下面的程序显示错误

MainActivity.java

package com.example.user.overflow;

import android.content.ClipData;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;


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) {
    RelativeLayout main_view = (RelativeLayout)     findViewById(R.id.main_view);

    switch (item.getItemId()){
        case R.id.menu_red:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.RED);
            return true;
        case R.id.menu_green:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.GREEN);
            return true;
        case R.id.menu_yellow:
            if(item.isChecked())
                item.setChecked(false);
            else
                item.setChecked(true);
            main_view.setBackgroundColor(Color.YELLOW);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main_view">

<TextView android:text="@string/hello_world"     android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

strings.xml

<resources>
<string name="app_name">Overflow</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="red_string">red</string>
<string name="green_string">green</string>
<string name="yellow_string">yellow</string>
</resources>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo">
    <!-- Customize your theme here. -->
</style>
<style name="LightThemeSelector" parent="android:Theme.Holo">
    <!-- Customize your theme here. -->
</style>

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.overflow" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
    <activity
        android:name=".MainActivity"
        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>

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="android:Theme.Holo">

</style>

<!-- ActionBar styles -->

</resources>

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"    tools:context=".MainActivity">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/menu_red"
        android:orderInCategory="1"
        app:showAsAction="never"
        android:title="@string/red_string" />
    <item
        android:id="@+id/menu_green"
        android:orderInCategory="2"
        app:showAsAction="never"
        android:title="@string/green_string" />
    <item
        android:id="@+id/menu_yellow"
        android:orderInCategory="3"
        app:showAsAction="never"
        android:title="@string/yellow_string" />
</group>

</menu>

logcat错误:

03-27 16:54:09.004      817-817/com.example.user.overflow E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.overflow/com.example.user.overflow.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
        at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
        at com.example.user.overflow.MainActivity.onCreate(MainActivity.java:16)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

您正在使用此主题进行所有活动

android:theme="@style/CustomActionBarTheme" 

这是一个整体主题。

尝试更改为

android:theme="@style/AppTheme" 

对于特定的活动

在styles.xml文件中进行一些编辑。在该文件中,您的父级当前是Base.Theme.AppCompat.Light.DarkActionBar,将其更改为Theme.AppCompat。 还将清单文件中的主题从style / CustomActionBarTheme更改为style / AppTheme。 现在,您可以轻松更改主题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM