设为首页 加入收藏

TOP

Android设置选项开发及自定义Preference样式(二)
2015-07-16 12:57:39 来源: 作者: 【 】 浏览:20
Tags:Android 设置 选项 开发 定义 Preference 样式
"right|center_vertical"
? ? ? ? android:textSize="18sp"
? ? ? ? android:layout_weight="1"/>



3) 继承Preference,实现自己的Preference类 PreferenceWithTip


public class PreferenceWithTip extends Preference {
? ? private static final String TAG = "PreferenceWithTip";
? ? String pTitle = null;
? ? String tipstring = null;
? ?
? ? @SuppressLint("Recycle")
? ? public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) {
? ? ? ? super(context, attrs, defStyle);
? ? ? ? // 获取自定义参数
? ? ? ? Log.i(TAG,"PreferenceWithTip invoked");
? ? ? ? TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip);
? ? ? ? tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring);
? ? ? ? pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring);
? ? ? ? ta.recycle();
? ? }


? ? public PreferenceWithTip(Context context, AttributeSet attrs) {
? ? ? ? this(context, attrs, 0);
? ? }


? ? @Override
? ? protected void onBindView(View view) {
? ? ? ? super.onBindView(view);
? ? ? ? TextView pTitleView = (TextView)view.findViewById(R.id.prefs_title);
? ? ? ? pTitleView.setText(pTitle);
? ? ? ? TextView pTipView = (TextView)view.findViewById(R.id.prefs_tip);
? ? ? ? pTipView.setText(tipstring);
? ? }


? ? @Override
? ? protected View onCreateView(ViewGroup parent) {
? ? ? ? return LayoutInflater.from(getContext()).inflate(R.layout.preferencewithtip,
? ? ? ? ? ? ? ? parent, false);
? ? }
? ?
? ? //如需更新、保存数据则需要继续编写
? ?
}


4) 调用。调用代码在文章的开头部分已经贴出,主要代码如下,preference是自定义的包名。


总结一下Preference的使用还是比较简单的,自定义Preference也比较方便。但是要设计出一个漂亮的、人性化的Preference还是不那么容易,但这些都是提高用户体验的途径,值得进一步挖掘。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发之打开闪光灯录制视频 下一篇Android平台上使用气压传感器计算..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: