2

Preference我可以随意设置所有项目的背景样式。如何将一些Pereference项目设置为一种风格(例如设置 a listSelector)并为其余Preference项目设置不同的风格?

我将样式设置为Preference设置其布局的样式:

<style name="Preference.Custom">
    <item name="android:layout">@layout/preference_custom</item>
    <item name="android:background">@color/transparent</item>
</style>

在中preference_custom.xml,我再次设置了背景:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/spacing_large"
    android:gravity="center_vertical"
    android:background="@color/transparent">     <--- Here's where I set the background

...

除按下状态的背景外,所有自定义样式都已设置。

我还尝试将背景设置为可绘制的,将按下状态设置为透明。

在我的 Preference 自定义类中,我设置了背景,我看到它从顶部条目变为底部条目,如下所示:

android.graphics.drawable.ColorDrawable@41e30248
android.graphics.drawable.ColorDrawable@41de71d8

这是我的子类Preference

public class PreferenceNoBackground extends Preference {
    private Context mContext;


    public PreferenceNoBackground(Context context) {
        super(context);
        mContext = context;
    }

    public PreferenceNoBackground(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public PreferenceNoBackground(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        view.setBackgroundResource(R.drawable.item_background_transparent);
    }
}

item_background_transparent.xml

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

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

    <item android:state_window_focused="false" android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@color/transparent" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="true"                                                             android:drawable="@color/transparent" />
    <item android:drawable="@color/transparent" />

</selector>

我还尝试从以下设置背景PreferenceFragment

    pref = findPreference(Settings.CUSTOM);
    pref.setOnPreferenceClickListener(this);
    pref.getView(null, null).setBackground(getResources().getDrawable(
      R.drawable.item_background_transparent));
4

1 回答 1

0

我之前通过对 2 个不同的列表项使用 2 个不同的布局来完成此操作。就我而言,我展示了一堆图像,然后在最后一个项目上显示了一个网站链接。我的适配器中的相关代码如下所示:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = null;

        if (position >= imgIds.size())
        {
            // Can we use the convertView? Only if it was expanded from R.layout.help_list_item_last...
            if (convertView != null && isLastView(convertView))
            {
                view = convertView;
            }

            if (view == null)
            { 
                view = inflater.inflate(R.layout.help_list_item_last, parent, false);
                if (view == null)
                    return null;
            }
        }
        else
        {
            ImageView helpImage = null;

            // Can we use the convertView? Only if it was expanded from R.layout.help_list_item...
            if (convertView != null && !isLastView(convertView))
            {
                view = convertView;
            }

            if (view == null)
            { 
                view = inflater.inflate(R.layout.help_list_item, parent, false);
                if (view == null)
                    return null;
            }
        }           
        return view; 
    }

    private boolean isLastView(View view)
    {
        if (view != null && view.findViewById(R.id.help_list_item_last) != null)
        {
            return true;
        }
        return false;
    }

为了更加灵活,代码可以不再使用 xml 布局,而是在 getView() 代码中动态设置每个列表的样式。就我而言,使用 2 种不同的布局很简单。

于 2013-06-18T19:03:51.543 回答