0

我有一个应用程序,其中我有一个偏好活动,它加载我的设置 xml。现在,我想做的是实现主题列表视图以启用应用程序。大多数情况下,我知道如何在设置 xml 中设置列​​表视图,但我不知道如何为应用程序启用不同的主题。谁能指出我正确的方向。或者有一些示例代码。

这是我的 Preference JAVA 文件,下面是我的设置 XML。

 public class Prefs extends PreferenceActivity {

ListPreference listPreference;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settingsbasic);
        //New 
        Preference customPref = (Preference) findPreference("clearcache");
        customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                                public boolean onPreferenceClick(Preference preference)    {
                                        Toast.makeText(getBaseContext(),
                                                        "Cache Cleared",
                                                        Toast.LENGTH_LONG).show();
                                        WebViewClientDemoActivity.web.clearCache(true);
                                        return false; 

                                }       

我的代码中还没有我的列表视图,任何关于如何实现的帮助都会很好。我只有一个清除缓存按钮。

这是我的设置 XML。

   <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference android:key="themes"
            android:title="@string/themes" 
            android:summary="@string/themes_list"
            android:defaultValue="true" />

    <CheckBoxPreference android:key="something"
             android:title="@string/cache" 
             android:summary="@string/somethingelse"
             android:defaultValue="true" />
    <Preference
            android:title="Clear Cache"
            android:summary="Hopefully this Works"
            android:key="clearcache" />

    </PreferenceScreen>

我有 2 个复选框,我想将第一个复选框更改为列表视图,并拥有一组主题列表。我只是不知道如何启用这些主题。我是否为每个主题创建单独的布局文件???或者我使用themes.xml,如果是这样,我如何通过单击列表视图中的对象来实现它们???

对不起所有的问题。任何帮助都会很棒。

main.xml 文件,我希望我的主题更改此 main.xml 的内容。大多数属性将保持不变。

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/boardimage"
  android:fadingEdge="vertical"
  android:orientation="vertical" >

  <ImageView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/boarder"
   android:contentDescription="@string/title"
   android:gravity="center_horizontal"
   android:src="@drawable/banner" >

 </ImageView> 

  <ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

 <WebView
   android:id="@+id/webview01"
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_gravity="center"
   android:layout_weight="1.12" >

  </WebView>



  </LinearLayout>

所以我想知道我将如何更改这个 xml 文件的内容。就像在我的蓝色主题上一样,我将使用不同的 drawable 作为背景或不同大小的 webview,或者在 imageview 中绘制图像。我该怎么做。

4

1 回答 1

2

如果您想了解主题和样式,请参阅 android developer 的此链接:http: //developer.android.com/guide/topics/ui/themes.html

构建两种样式,然后从 prefrence 中给出设置值并在主题之间切换,您可以使用以下代码在您的活动中以编程方式设置主题:setTheme(R.style.MyTheme);

您可以在 values 文件夹中的 theme.xml 文件中使用以下代码来执行此操作:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="@android:style/Theme.Light">

        <!-- hide the Window Title -->
        <item name="android:windowNoTitle">true</item>

    </style>

</resources>

您可以使用此代码创建 ListPreference 并在 String.xml 中添加您的字符串数组:

 <ListPreference
  android:title="List Preference"
  android:summary="Select the option of the list"
  android:key="listPref"
  android:entries="@array/listDisplayWord"
  android:entryValues="@array/listReturnValue" />

此链接对您很有用:How to add multiple theme attributes to the same Activity on Android Manifest?

您可以使用 listPreference 提供的值并在布局之间切换,如下所示:

if(myTheme==1)
     setContentView(R.layout.main1);
else
     setContentView(R.layout.main2);

但这些布局中的组件(按钮、textViews、EditText、...)的名称必须相同。

于 2012-07-24T18:00:14.243 回答