2011年6月27日月曜日

Android 左側にマークを置ける CheckedTextView を作った

フレームワークの CheckedTextView はチェックマークの場所が右側固定なので、左にもできるようにしたカスタムビューを作りました。

CheckedTextView - yanzm-s-Custom-View-Project at github -

使い方はこんな感じ

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res/yanzm.products.customview"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent">  
  8.     
  9.     <yanzm.products.customview.CustomCheckedTextView   
  10.         android:text="CustomCheckedTextView"   
  11.         android:layout_width="fill_parent"   
  12.         android:layout_height="?android:attr/listPreferredItemHeight"  
  13.         android:textAppearance="?android:attr/textAppearanceLarge"  
  14.         android:gravity="center_vertical"  
  15.         android:paddingLeft="30dip"  
  16.         android:paddingRight="6dip"          
  17.         app:checkMark="?android:attr/textCheckMark"  
  18.         app:checked="true"  
  19.         />  
  20.            
  21.     <yanzm.products.customview.CustomCheckedTextView   
  22.         android:text="CustomCheckedTextView"   
  23.         android:layout_width="fill_parent"   
  24.         android:layout_height="?android:attr/listPreferredItemHeight"  
  25.         android:textAppearance="?android:attr/textAppearanceLarge"  
  26.         android:gravity="center_vertical"  
  27.         android:paddingLeft="30dip"  
  28.         android:paddingRight="6dip"          
  29.         app:checkMark="?android:attr/textCheckMark"  
  30.         app:checked="true"  
  31.         app:drawablePadding="10dip"  
  32.         />  
  33.            
  34.            
  35.     <yanzm.products.customview.CustomCheckedTextView   
  36.         android:text="CustomCheckedTextView"   
  37.         android:layout_width="fill_parent"   
  38.         android:layout_height="?android:attr/listPreferredItemHeight"  
  39.         android:textAppearance="?android:attr/textAppearanceLarge"  
  40.         android:gravity="center_vertical"  
  41.         android:paddingLeft="40dip"  
  42.         android:paddingRight="6dip"          
  43.         app:checkMark="?android:attr/textCheckMark"  
  44.         app:checked="true"  
  45.         app:position="right"  
  46.          />  
  47.            
  48.     <yanzm.products.customview.CustomCheckedTextView   
  49.         android:text="CustomCheckedTextView"   
  50.         android:id="@+id/customCheckedTextView1"   
  51.         android:layout_width="fill_parent"   
  52.         android:layout_height="?android:attr/listPreferredItemHeight"  
  53.         android:textAppearance="?android:attr/textAppearanceLarge"  
  54.         android:gravity="center_vertical"  
  55.         android:paddingLeft="40dip"  
  56.         android:paddingRight="6dip"          
  57.         app:checkMark="?android:attr/textCheckMark"  
  58.         app:checked="true"  
  59.         app:position="right"  
  60.         app:drawablePadding="30dip"  
  61.         />           
  62.            
  63.     <CheckedTextView   
  64.         android:text="CustomCheckedTextView"   
  65.         android:id="@+id/customCheckedTextView1"   
  66.         android:layout_width="fill_parent"   
  67.         android:layout_height="?android:attr/listPreferredItemHeight"  
  68.         android:textAppearance="?android:attr/textAppearanceLarge"  
  69.         android:gravity="center_vertical"  
  70.         android:checkMark="?android:attr/textCheckMark"  
  71.         android:paddingLeft="40dip"  
  72.         android:paddingRight="6dip"          
  73.         android:checked="true"  
  74.         />  
  75.            
  76. </LinearLayout>  






設定できるパラメータは

  • checkMark : reference チェックマークのリソースID
  • checked : boolean 初期状態
  • position : "left | right" チェックマークの位置
  • drawablePadding : dimension チェックマークと文字の間の余白


です。
Android Library Project としてインポートして使えます。


 

0 件のコメント:

コメントを投稿