Android Studio - 应用内更改主题按钮?

时间:2015-03-16 16:51:56

标签: java android xml android-activity android-studio

希望你过得愉快!

所以我在Android Studio中创建了一个基本的应用程序,但我不是初学者。我想让用户只需按一下按钮即可更改应用程序的主题,并在某种程度上遵循本指南:http://www.developer.com/ws/android/changing-your-android-apps-theme-dynamically.html

但我自己做了一些改变。我现在的问题是,当我使用黑色主题时,背景变为黑色(正确),textColor变为白色(正确)但我的单选按钮的文本颜色和按钮本身保持黑色,无法看到。

如何在更改背景/普通textColor的同时更改单选按钮和单选按钮文本的颜色?

Styles.xml

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

    <!-- White theme -->
    <style name="WhiteTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

    <!-- Black theme -->
    <style name="BlackTheme" >
        <item name="android:background">#000000</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <!-- Blue theme -->
    <style name="BlueTheme" >
        <item name="android:background">#B0E0E6</item>
        <item name="android:textColor">#000000</item>
    </style>

    <!-- Pink theme -->
    <style name="PinkTheme">
        <item name="android:background">#FF69B4</item>
        <item name="android:textColor">#000000</item>
    </style>
</resources>

EU.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="com.example.xxx.zzz.EU">

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </RadioGroup>
    <RadioButton android:id="@+id/EUManRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/EUManRadioButtonString"
        android:onClick="onRadioButtonClicked"
        android:layout_centerVertical="true"/>

    <RadioButton android:id="@+id/EUWomanRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/EUWomanRadioButtonString"
        android:onClick="onRadioButtonClicked"
        android:layout_below="@+id/EUManRadioButton"
        android:layout_alignLeft="@+id/EUManRadioButton"
        android:layout_alignStart="@+id/EUManRadioButton" />
</RelativeLayout>

Android Manifest:     

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>
    <activity
        android:name=".ChangeTheme"
        android:label="@string/title_activity_change_theme" >
    </activity>
    <activity
        android:name=".About"
        android:label="@string/title_activity_about" >
    </activity>
    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" >
    </service>
</application>
</manifest>

ChangeTheme.java

package com.example.xxx.zzz;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class ChangeTheme extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_theme);
}

public void onClickChangeThemeBack(View view){
    Intent i1 = new Intent(this, MainActivity.class);
    startActivity(i1);
}

public void onClickNormalTheme(View view){
//Placeholder BLACK    
themeUtils.changeToTheme(this, themeUtils.BLACK);
}

public void onClickBlackTheme(View view){
    themeUtils.changeToTheme(this, themeUtils.BLACK);
}

public void onClickBlueTheme(View view){
    themeUtils.changeToTheme(this, themeUtils.BLUE);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_u, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

activity_change_theme.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="com.example.xxx.zzz.ChangeTheme">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ChangeThemeBackButtonString"
    android:id="@+id/ChangeThemeBackButton"
    android:onClick="onClickChangeThemeBack"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ChangeThemeButtonString"
    android:id="@+id/button"
    android:onClick="onClickChangeThemeBack"
    android:layout_alignTop="@+id/ChangeThemeBackButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ChangeThemeNormalButtonString"
    android:id="@+id/ChangeThemeNormalButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="onClickNormalTheme" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ChangeThemeBlackButtonString"
    android:id="@+id/ChangeThemeBlackButton"
    android:layout_marginTop="50dp"
    android:layout_below="@+id/ChangeThemeNormalButton"
    android:layout_centerHorizontal="true"
    android:onClick="onClickBlackTheme" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ChangeThemeBlueButtonString"
    android:id="@+id/ChangeThemeBlueButton"
    android:layout_marginTop="50dp"
    android:layout_below="@+id/ChangeThemeBlackButton"
    android:layout_centerHorizontal="true"
    android:onClick="onClickBlueTheme" />

</RelativeLayout>

themeUtils.java

package com.example.xxx.zzz;

import android.app.Activity;
import android.content.Intent;

public class themeUtils
{
    private static int cTheme;
    public final static int WHITE = 0;
    public final static int BLACK = 1;
    public final static int BLUE = 2;
    public final static int PINK = 3;

    public static void changeToTheme(Activity activity, int theme)
    {
        cTheme = theme;
        activity.finish();
        activity.startActivity(new Intent(activity, activity.getClass()));
    }

    public static void onActivityCreateSetTheme(Activity activity)
    {
        switch (cTheme)
        {
            default:
            case WHITE:
                activity.setTheme(R.style.WhiteTheme);
                break;
            case BLACK:
                activity.setTheme(R.style.BlackTheme);
                break;
            case BLUE:
                activity.setTheme(R.style.BlueTheme);
                break;
            case PINK:
                activity.setTheme(R.style.PinkTheme);
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将此添加到您希望在values / styles.xml或主题文件的任何位置提供给用户的每个主题

<?xml version="1.0" encoding="utf-8"?>   <resources>
<style name="Theme" parent="@android:style/Theme">
     <!-- Widget styles -->
     <item name="android:spinnerStyle">@android:style/Widget.Spinner</item>
     <item      name="android:spinnerDropDownItemStyle">@style/Widget.DropDownItem.Spinner </item>
     <item   name="android:spinnerItemStyle">@android:style/Widget.TextView.SpinnerItem </item>
</style>