应用复选框样式在Android的preferenceActivity [英] Applying Checkbox Styles in Android PreferenceActivity

查看:100
本文介绍了应用复选框样式在Android的preferenceActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,SDK 10+。我想在我的活动我所有的复选框,以及我的preferenceActivity看起来相同。我创建一个具有Theme.Black父主题。我创建的Andr​​oid的子风格:Widget.CompoundButton.CheckBox并设置了android:checkboxStyle到子主题。我已经申请我的主题在整个应用程序在应用程序清单的元素搭配:

 安卓主题=@风格/ AppTheme

在我看主要活动,复选框样式已应用于如预期,但在preferenceActivity复选框没有风格。我的问题是:


  • 是否有可能样式preferences的小部件,而不必指定为每一个一个全新的布局?

  • 更普遍,我想为在preferences显示出来,就像切换开关或其他控件做到这一点。那可能吗? (我的猜测是,解决一会pretty多少能够被应用到那些特定的问题,以及)

我知道有嵌套preferences一个问题,但这个问题是适用于preferences在顶层。任何帮助是极大的AP preciated。我一直在敲我的头撞墙就这一个,现在几个小时......

下面是我到目前为止有:

prefs.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <复选框preference
        机器人:键=checkbox1
        机器人:汇总=这是一个主题复选框preference
        机器人:标题=复选框1/>< / preferenceScreen>

主题preferenceActivity.java

 公共类主题preferencesActivity扩展$ P $ {pferenceActivity
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml preFS);
    }
}

main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <复选框
        机器人:ID =@ + ID / checkBox1
        机器人:文字=复选框
        安卓:检查=真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT的风格=@风格/ AppTheme preference.Checkbox/>
< / LinearLayout中>

MainActivity.java

 公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }}

style.xml

 <资源的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <样式名称=AppTheme父=机器人:Theme.Black>
        <项目名称=机器人:checkboxStyle> @风格/ AppTheme preference.Checkbox< /项目>
    < /风格>    <样式名称=AppTheme preference.Checkbox。家长=机器人:Widget.CompoundButton.CheckBox>
        <项目名称=机器人:按钮> @绘制/ pref_checkbox< /项目>
        <项目名称=机器人:背景>#FF0000< /项目>
    < /风格>
< /资源>

pref_checkbox.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:state_checked =真正的机器人:可绘制=@机器人:可绘制/ ic_menu_call/>
    <项目的android:state_checked =false的机器人:可绘制=@机器人:可绘制/ ic_menu_camera/>
< /选择>


解决方案

看起来像问题是与行:

 <样式名称=AppTheme preference.Checkbox。家长=机器人:Widget.CompoundButton.CheckBox>
    <项目名称=机器人:按钮> @绘制/ pref_checkbox< /项目>
    <项目名称=机器人:背景>#FF0000< /项目> < ----在这里
< /风格>

看起来指定是造成该复选框胡作非为。事实证明,我没有必要说反正(事实上,意识到我会做的ListView控件的风格无论如何,如果我想它),这样,问题就解决了​​。

I am writing an application that SDK 10+. I would like all of my check boxes in my Activities as well as my PreferenceActivity to look the same. I created a Theme that has a Theme.Black parent. I created a child style of android:Widget.CompoundButton.CheckBox and set the android:checkboxStyle to that child theme. I have applied my theme across the entire app in the application manifest element with:

android:theme="@style/AppTheme"

When I look at the main Activity, the check box style has been applied as expected, but the checkbox in the PreferenceActivity does not have the style. My questions are:

  • Is it possible to style minor parts of preferences without having to specify an entire new layout for each one?
  • More generally, I would like to do this for other controls that show up in the preferences, like toggles or switches. Is that possible? (My guess is that the solution to one will pretty much be able to be applied to those particular issues as well)

I know there is an issue for nested preferences, but this question is applicable to Preferences at the top level. Any help is greatly appreciated. I have been banging my head against a wall on this one for a couple hours now...

Here is what I have so far:

prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:key="checkbox1"
        android:summary="This is a themed checkbox preference"
        android:title="Checkbox 1" />

</PreferenceScreen>

ThemedPreferenceActivity.java

public class ThemedPreferencesActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:text="CheckBox"
        android:checked="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" style="@style/AppTheme.Preference.Checkbox" />


</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

}

style.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="android:Theme.Black">    
        <item name="android:checkboxStyle">@style/AppTheme.Preference.Checkbox</item>
    </style>

    <style name="AppTheme.Preference.Checkbox" parent="android:Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/pref_checkbox</item>
        <item name="android:background">#FF0000</item>
    </style>
</resources>

pref_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@android:drawable/ic_menu_call" />
    <item android:state_checked="false" android:drawable="@android:drawable/ic_menu_camera" />
</selector>

解决方案

Looks like the issue was with the line:

<style name="AppTheme.Preference.Checkbox" parent="android:Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/pref_checkbox</item>
    <item name="android:background">#FF0000</item> <---- RIGHT HERE
</style>

Looks like specifying that was causing the check box to misbehave. Turns out that I didn't need that anyway (and in fact, realized I would do it in the "ListView" style anyway if I wanted it) so, problem solved.

这篇关于应用复选框样式在Android的preferenceActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆