Android的设置自定义preference布局 [英] Android Set Custom Preference Layout

查看:145
本文介绍了Android的设置自定义preference布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作就可以了Android项目。我有一个prefs.xml code,像这样

I'm working on it Android project. I have a prefs.xml code, something like this

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

        <Preference
            android:key="pref_name_color_picker"
            android:title="Colour"
            android:summary="Colour of the name"
            android:defaultValue="#FFFFFF"
            android:layout="@layout/custom_name_setting_layout" />
    </PreferenceCategory>


</PreferenceScreen>

我需要定制preference布局。我创建的;

And I need to custom preference layout. And I created;

custom_name_setting_layout.xml

custom_name_setting_layout.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="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

        <ImageView
            android:id="@+id/ivNameTextColor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="32dp"
            android:minWidth="32dp"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

</LinearLayout>

和写SettingActivity.java

And write a SettingActivity.java

public class SettingActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
    int color = 0xffffff00;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);


       LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.custom_name_setting_layout, null);

        ImageView ivNameTextColor = (ImageView) row.findViewById(R.id.ivNameTextColor);
        ivNameTextColor.setBackgroundColor(Color.RED);
    }
}

我的问题是;我写setBackgroundColor方法,但不能正常工作。不工作meanin是,这个程序是没有错误运行(如NullReferenceException异常,没有错误)。但是,背景颜色还是没有改变。

My problem is; I write setBackgroundColor method but not working. Not working meanin is, this program is running without error (like NullReferenceException, there is no error). But background color still not changing.

我不知道为什么。我怎样才能解决这个问题?
谢谢

I don't know why. How can i solve this problem? Thanks

推荐答案

显然,如果你硬编码的颜色,那么你可以做在你的XML:

Obviously, if you are hard-coding the color then you can just do it in your XML:

android:background="@android:color/red"

如果你想这样做在code,那么不幸的是它的麻烦比它看起来。你不能只是设置preference视图的颜色的onCreate()因为preference意见被存储在一个列表,并动态创建和回收当您滚动列表。

If you want to do it in code then unfortunately it's trickier than it might seem. You can't just set the color of the preference view in onCreate() because the preference views are stored in a list and are created and recycled dynamically as you scroll the list.

您需要设置背景颜色正在创建视图时。要做到这一点,你需要实现一个自定义的preference类和重写 getView()

You need to set the background color when the view is being created. To do that you'll need to implement a custom preference class and override getView():

public class CustomColorPreference extends Preference
{
    int backgroundColor = Color.BLACK;

    public CustomColorPreference(Context context) {
        super(context);
    }

    public CustomColorPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setCustomBackgroundColor(int color)
    {
        backgroundColor = color;
    }

    @Override
    public View getView(View convertView, ViewGroup parent)
    {
        View v = super.getView(convertView, parent);

        // v.setBackgroundColor(backgroundColor); // set background color of whole view
        ImageView ivNameTextColor = (ImageView)v.findViewById(R.id.ivNameTextColor);
        ivNameTextColor.setBackgroundColor(backgroundColor);

        return v;
    }
}

修改XML使用 CustomColor preference 类:

<com.example.yourapp.CustomColorPreference
        android:key="pref_name_color_picker"
        android:title="Colour"
        android:summary="Colour of the name"
        android:defaultValue="#FFFFFF"
        android:layout="@layout/custom_name_setting_layout" />

然后在你的的onCreate 就可以得到 CustomColor preference 并设置颜色使用它的上公共方法 setCustomBackgroundColor()

Then in your onCreate you can get the CustomColorPreference and set the color on it using the public method setCustomBackgroundColor():

CustomColorPreference picker = (CustomColorPreference)findPreference("pref_name_color_picker");
picker.setCustomBackgroundColor(Color.RED);

这篇关于Android的设置自定义preference布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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