新闻中心

EEPW首页>嵌入式系统>设计应用> Android设置选项开发及自定义Preference样式

Android设置选项开发及自定义Preference样式

作者: 时间:2016-09-12 来源:网络 收藏

2

本文引用地址://m.amcfsurvey.com/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还是不那么容易,但这些都是提高用户体验的途径,值得进一步挖掘。


上一页 1 2 下一页

关键词:

评论


相关推荐

技术专区

关闭