java - 在 Peference Activity 内,创建要为应用程序启用的主题 ListView

标签 java android android-listview themes preferenceactivity

我有一个应用程序,其中我有一个加载我的设置 xml 的偏好 Activity 。现在,我想做的是实现主题 ListView 以启用应用程序。我知道如何在设置 xml 中设置列​​ TableView ,但我不知道如何为应用程序启用不同的主题。有人能指出我正确的方向吗?或者有一些示例代码。

这是我的首选项 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; 

                                }       

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

这是我的设置 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 个复选框,我想将第一个复选框更改为 ListView ,并有一个主题列表数组。我只是不知道如何启用这些主题。我是否为每个主题创建单独的布局文件???或者所以我使用themes.xml,如果是这样我如何通过单击 ListView 中的对象来实现这些???

很抱歉提出所有问题。任何帮助都会很棒。

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 文件的内容。就像我的蓝色主题一样,我将使用不同的可绘制对象作为背景或不同大小的 WebView ,或者在 ImageView 中使用可绘制的图像。我该怎么做。

最佳答案

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

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

您可以使用values文件夹中的themes.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...)必须相同。

关于java - 在 Peference Activity 内,创建要为应用程序启用的主题 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11636323/

相关文章:

java - SQLite 数据库错误 [致命异常错误]

java - image.setImageDrawable 导致应用程序崩溃

android - 如何使 ListView 可滚动直到最后一个元素位于屏幕顶部

java - android - 在调试时关闭乱舞

Java 容器未显示

android - 如何在 osmdroid 中添加标记?

android - 将项目符号添加到 Android 的 ListView

android - 如何将按钮添加到数组列表

java - 使用递归来匹配带有通配符的字符串

java - Android getSupportedPreviewSizes() on Camera 仅返回 3 个尺寸