java-Android设置自定义首选项布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-Android设置自定义首选项布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4095字,纯文字阅读大概需要6分钟。
内容图文
![java-Android设置自定义首选项布局](/upload/InfoBanner/zyjiaocheng/690/8d2baa4b465946168380bfbde41a05a1.jpg)
我正在研究Android项目.我有一个prefs.xml代码,像这样
<?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>
而且我需要自定义首选项布局.我创造了;
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
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方法,但是没有用.不起作用的意思是,该程序正在运行而没有错误(例如NullReferenceException,没有错误).但是背景颜色仍然没有改变.
我不知道为什么我怎么解决这个问题?
谢谢
解决方法:
显然,如果您要对颜色进行硬编码,则可以在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并使用公共方法setCustomBackgroundColor()在其上设置颜色:
CustomColorPreference picker = (CustomColorPreference)findPreference("pref_name_color_picker");
picker.setCustomBackgroundColor(Color.RED);
内容总结
以上是互联网集市为您收集整理的java-Android设置自定义首选项布局全部内容,希望文章能够帮你解决java-Android设置自定义首选项布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。