栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android设置自定义首选项布局

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android设置自定义首选项布局

显然,如果您要对颜色进行硬编码,则可以在XML中进行编码:

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

如果您想在代码中执行此操作,那么不幸的是,它比看起来更棘手。您不能只设置首选项视图的颜色,

onCreate()
因为首选项视图存储在列表中,并在滚动列表时动态创建和回收。

创建视图时,需要设置背景颜色。为此,您需要实现一个自定义首选项类并重写

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以使用

CustomColorPreference
该类:

<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
可以
CustomColorPreference
使用public方法获取并为其设置颜色
setCustomBackgroundColor()

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/451688.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号