天天看點

Android中自定義控件

自定義了一個Button按鈕,效果如圖,有自定義背景(可根據文字長短擴充),自定義按鈕圖檔和文字背景圖檔擴充,參考http://www.7747.net/kf/201104/88131.html 這個文章,http://blog.csdn.net/greatstar/archive/2010/10/12/5936177.aspx 和這個文章。使用draw9patch時遇到不能啟動,原因是Android 2.3少了一個包,swing-worker-1.2 下載下傳放在android-sdk\tools\lib 下遍可以正常使用。

 其他直接上代碼

?[Copy to clipboard]Download zuiniuwang.java

  1. <span style="font-size: 14px">import android.content.Context;  
  2. import android.view.ViewGroup;  
  3. import android.widget.ImageView;  
  4. import android.widget.LinearLayout;  
  5. import android.widget.RelativeLayout;  
  6. import android.widget.TextView;  
  7. public class MyViewTest extends RelativeLayout {  
  8.     private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;  
  9.     private final int FP = LinearLayout.LayoutParams.FILL_PARENT;  
  10.     private final static  String TAG = MyViewTest.class.getSimpleName();  
  11.     /**  
  12.      *  
  13.      * @param context  
  14.      * @param backgroudPic  背景圖檔的ID  為空則為預設  
  15.      * @param p_w_picpathPic     按鈕前面的圖檔   為空則為預設  
  16.      * @param text         按鈕的文本             為空則為預設  
  17.      */  
  18.     public MyViewTest(Context context, Integer backgroudPic , Integer p_w_picpathPic , String text) {  
  19.         super(context);  
  20.         // TODO Auto-generated constructor stub  
  21.         if(backgroudPic==null)  
  22.             this.setBackgroundResource(R.drawable.my_music_button);  
  23.         else  
  24.             this.setBackgroundResource(backgroudPic);  
  25.         ImageView  p_w_picpathView =  new ImageView(context);  
  26.         if(p_w_picpathPic==null)  
  27.             p_w_picpathView.setImageResource(R.drawable.style1_button_icon_normal);  
  28.         else  
  29.             p_w_picpathView.setImageResource(p_w_picpathPic);  
  30.         p_w_picpathView.setId(1);  
  31.         RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
  32.                 ViewGroup.LayoutParams.WRAP_CONTENT);  
  33.         lp1.addRule(RelativeLayout.RIGHT_OF , 1);  
  34.         TextView  textView =  new TextView(context);  
  35.         if(text==null)  
  36.             textView.setText("my custom button");  
  37.         else  
  38.             textView.setText(text);  
  39.         this.addView(p_w_picpathView);  
  40.         this.addView(textView , lp1);  
  41.     }  
  42. }  

?[Copy to clipboard]Download zuiniuwang.xml

  1. <span style="font-size: 14px"><?xml version="1.0" encoding="UTF-8"?> 
  2. </span> 
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android"><span style="font-size: 14px"> 
  4.     </span><item android:drawable="@drawable/my_button_bg" android:state_pressed="true"><span style="font-size: 14px"> 
  5.     </span><item android:drawable="@drawable/my_button_bg" android:state_focused="true"><span style="font-size: 14px"> 
  6.     </span><item android:drawable="@drawable/my_music_button_bg"><span style="font-size: 14px"> 
  7. </span></item></item></item></selector><span style="font-size: 14px"> 
  8. </span>