"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还是不那么容易,但这些都是提高用户体验的途径,值得进一步挖掘。