新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > Android設置選項開發及自定義Preference樣式

        Android設置選項開發及自定義Preference樣式

        作者: 時間:2016-09-12 來源:網絡 收藏

        2

        本文引用地址:http://www.104case.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 下一頁

        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 筠连县| 随州市| 东阿县| 金华市| 蕉岭县| 花垣县| 个旧市| 桐柏县| 宜丰县| 新野县| 江山市| 武山县| 凉山| 屯昌县| 济宁市| 秀山| 儋州市| 英德市| 林西县| 北流市| 墨玉县| 洪洞县| 丰城市| 马山县| 合江县| 丹东市| 贵溪市| 泗水县| 毕节市| 和顺县| 浮山县| 汪清县| 宜黄县| 丽水市| 汝南县| 漳州市| 彩票| 张掖市| 余干县| 庐江县| 沧州市|