gpt4 book ai didi

android - 使用列表首选项为应用程序添加主题支持

转载 作者:行者123 更新时间:2023-11-30 03:11:06 25 4
gpt4 key购买 nike

我正在尝试为应用程序添加主题支持。它正在工作,但我希望用户在主题之间进行选择。问题是,我做不到,我尝试了很多东西。我正在使用 ListPreference 来定义一个数组列表供用户选择。我无法将这些 listpreference 条目值与 util 链接起来。如果我在 Util 中用任何数字编辑“0”,主题会起作用,但当我更改这些条目值(从电话列表中)时它不起作用 IDK 为什么。下面是代码

Settings.java

 public class Settings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Util.setAppTheme(this);
super.onCreate(savedInstanceState);

// Display the fragment as the main content.
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
PrefsFragment mPrefsFragment = new PrefsFragment();
mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
mFragmentTransaction.commit();


}

public static class PrefsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}

}

Util.java

public class Util extends Activity {

public static void setAppTheme(Activity a) {

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(a);
int mTheme = Integer.parseInt(sp.getString("theme", "0"));
if(mTheme==0)
{

a.setTheme(R.style.Dark);
}
if(mTheme==1)
{
a.setTheme(R.style.Light);
}
if(mTheme==2)
{
a.setTheme(R.style.LimeLight);
}
if(mTheme==3)
{
a.setTheme(R.style.MojoLight);
}
if(mTheme==4)
{
a.setTheme(R.style.SanMarinoLight);
}
if(mTheme==5)
{
a.setTheme(R.style.LimeDark);
}
if(mTheme==6)
{
a.setTheme(R.style.MojoDark);
}
if(mTheme==7)
{
a.setTheme(R.style.SanMarinoDark);
}

}
}

preferences.xml

<PreferenceCategory
android:title="@string/preference_category2_title">
<ListPreference android:key="list2_preference"
android:title="@string/list2_title"
android:summary="@string/list2_summary"
android:entries="@array/list2_preferences"
android:entryValues="@array/list2_preferences_values"
android:dialogTitle="@string/list2_dialog_title"/>
<SwitchPreference android:key="switch1_preference"
android:title="@string/switch1_title"
android:switchTextOff="@string/switch1_textoff"
android:switchTextOn="@string/switch1_texton"
/>
</PreferenceCategory>

styles.xml

<!-- Dark -->
<style name="Dark" parent="android:Theme.Holo">

<item name="android:windowBackground">@drawable/activity_background_dark</item>
</style>

<!-- Light -->
<style name="Light" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBar_Light</item>
<item name="android:windowBackground">@drawable/activity_background_light</item>

</style>

数组.xml

<resources>

<string-array name="list2_preferences">
<item>Dark</item>
<item>Light</item>
<item>Light Lime</item>
<item>Light Mojo</item>
<item>Light San Marino</item>
<item>Dark Lime</item>
<item>Dark Mojo</item>
<item>Dark San Marino</item>
</string-array>

<string-array name="list2_preferences_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>

</string-array>

如果有人能帮助我,我将不胜感激?

最佳答案

您的列表的 onItemSelected 方法的代码是什么?

此外,您无法在初始化布局后更改 Activity 的主题。在我当前的应用程序中,我还允许用户更改主题。为实现这一点,我只需再次启动 Activity 并传递一个参数(选定的主题)并完成当前的 Activity 。然后在 onCreate() Activity 中检查参数。如果它是 -1( Activity 第一次启动),我将获得默认主题。

private static final String INTENT_EXTRA = "theme";
private boolean onCreate;

@Override
protected void onCreate(Bundle savedInstanceState) {
onCreate = true;

themeId = getIntent().getIntExtra(INTENT_EXTRA, -1);
if (themeId == -1) {
themeId = SharedPreferencesManagment
.getApplicationThemeResourceId(SharedPreferencesManagment
.getIntApplicationTheme(this));
} else {
themeId = SharedPreferencesManagment
.getApplicationThemeResourceId(themeId);
}
setTheme(themeId);

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_options_theme);

initSpinner();
}

在列表的 onItemSelected() 方法中,我调用如下内容:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!onCreate) {
Intent intent = new Intent(this, OptionsThemeActivity.class);
intent.putExtra(INTENT_EXTRA, getSelectedTheme());
startActivity(intent);

finish();
}
onCreate = false;
}

另请注意 onCreate bool 值,因为 onItemSelected() 方法在 Activity 初始​​化微调器时也会被调用。(是防止无限循环的最简单的解决方法)

还有:为什么 Util 是一个 Activity?如果您发布的代码是您使用 Util 执行的所有操作,则您无需在传递上下文时扩展 Activity。

关于android - 使用列表首选项为应用程序添加主题支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20965723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com