Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置首选项屏幕中某些项目的背景样式_Android - Fatal编程技术网

Android 如何设置首选项屏幕中某些项目的背景样式

Android 如何设置首选项屏幕中某些项目的背景样式,android,Android,我可以根据自己的喜好为所有首选项项目设置背景样式。我如何将一些Pereference项设置为一种样式(例如,设置列表选择器),并使用不同样式设置其余首选项项的样式 我将首选项的样式设置为设置其布局的样式: <style name="Preference.Custom"> <item name="android:layout">@layout/preference_custom</item> <item name="android:back

我可以根据自己的喜好为所有
首选项
项目设置背景样式。我如何将一些
Pereference
项设置为一种样式(例如,设置
列表选择器
),并使用不同样式设置其余
首选项
项的样式

我将
首选项的样式设置为设置其布局的样式:

<style name="Preference.Custom">
    <item name="android:layout">@layout/preference_custom</item>
    <item name="android:background">@color/transparent</item>
</style>
将设置除按下状态的背景之外的所有自定义样式

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

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

android.graphics.drawable.ColorDrawable@41e30248
android.graphics.drawable.ColorDrawable@41de71d8
下面是我的
首选项的子类

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\u background\u 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>

我以前曾为两个不同的列表项使用过两种不同的布局。在我的例子中,我展示了一堆图片,然后在最后一个项目上显示了一个网站链接。我的适配器中的相关代码如下所示:

@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()代码中动态设置每个列表的样式。在我的例子中,使用两种不同的布局很简单

    pref = findPreference(Settings.CUSTOM);
    pref.setOnPreferenceClickListener(this);
    pref.getView(null, null).setBackground(getResources().getDrawable(
      R.drawable.item_background_transparent));
@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;
    }