preference android:layout,Android设置选项开发及自定义Preference样式

2本文引用地址:http://www.eepw.com.cn/article/201609/304509.htm

3

4

5

6

7

复制代码

2) 设计自定义Preference的布局 preferencewithtip.xml

1

2

3 android:layout_width=match_parent

4 android:layout_height=match_parent

5 android:orientation=horizontal

6 android:paddingLeft=8dp

7 android:paddingRight=15dp

8 android:paddingTop=20dp

9 android:paddingBottom=20dp>

10

11 android:id=@+id/prefs_title

12 android:layout_width=0dp

13 android:layout_height=wrap_content

14 android:layout_gravity=left

15 android:gravity=left|center_vertical

16 android:textSize=18sp

17 android:layout_weight=1/>

18

19 android:id=@+id/prefs_tip

20 android:layout_width=0dp

21 android:layout_height=wrap_content

22 android:layout_gravity=right

23 android:gravity=right|center_vertical

24 android:textSize=18sp

25 android:layout_weight=1/>

26

27

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

1 public class PreferenceWithTip extends Preference {

2 private static final String TAG = PreferenceWithTip;

3 String pTitle = null;

4 String tipstring = null;

5

6 @SuppressLint(Recycle)

7 public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) {

8 super(context, attrs, defStyle);

9 // 获取自定义参数

10 Log.i(TAG,PreferenceWithTip invoked);

11 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip);

12 tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring);

13 pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring);

14 ta.recycle();

15 }

16

17 public PreferenceWithTip(Context context, AttributeSet attrs) {

18 this(context, attrs, 0);

19 }

20

21 @Override

22 protected void onBindView(View view) {

23 super.onBindView(view);

24 TextView pTitleView = (TextView)view.findViewById(R.id.prefs_title);

25 pTitleView.setText(pTitle);

26 TextView pTipView = (TextView)view.findViewById(R.id.prefs_tip);

27 pTipView.setText(tipstring);

28 }

29

30 @Override

31 protected View onCreateView(ViewGroup parent) {

32 return LayoutInflater.from(getContext()).inflate(R.layout.preferencewithtip,

33 parent, false);

34 }

35

36 //如需更新、保存数据则需要继续编写

37

38 }

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

复制代码

1

2 preference:tipstring=>

3 preference:titlestring=自定义测试 >

4

5 android:action=android.intent.action.VIEW

6 android:data=http://www.baidu.com />

7

复制代码

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值