当前位置:编程学习 > wap >>

自定义button问题,仿360手机卫士界面

我自定义了button,仿360手机卫士,问题:button之间没有空格,求指点
public class MyImageButton extends LinearLayout {

 public MyImageButton(Context context, int imageResId, int textResId) {
    super(context); 
   
    mButtonImage = new ImageView(context);   
    mButtonText  = new TextView(context);
    setImageResource(imageResId); 
    mButtonImage.setPadding(0, 0, 0, 0);
  
    
    setText(textResId);
    setTextColor(0xFF000000);
    mButtonText.setPadding(0, 0, 0, 0);
      
    //设置布局的属性
    setClickable(true);
    setFocusable(true);

    setOrientation(LinearLayout.VERTICAL);

    addView(mButtonImage);
    addView(mButtonText);

 }
   // ----------------public method----------------------------- 
      /* 
        * setImageResource方法 40.   */ 
        public void setImageResource(int resId) { 
                  mButtonImage.setImageResource(resId); 
          } 
          /* 46.   * setText方法 47.   */ 
         public void setText(int resId) { 
           mButtonText.setText(resId); 
           } 
  public void setText(CharSequence buttonText) {
    mButtonText.setText(buttonText); 

  /* 57.   * setTextColor方法 58.   */ 
  public void setTextColor(int color) { 
    mButtonText.setTextColor(color); 
 } 
  
  // ----------------private attribute----------------------------- 
  private ImageView mButtonImage = null; 
  private TextView mButtonText = null;   
}
--------------------编程问答-------------------- <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
     android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/background"> 
   <!-- 第一层按钮” --> 
   <LinearLayout
       android:id="@+id/line1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
       android:orientation="horizontal" >
       <LinearLayout
       android:id="@+id/bt1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
       android:orientation="vertical"
      
        >     
           </LinearLayout>
       <LinearLayout
       android:id="@+id/bt2"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
        android:orientation="vertical"
       >                  
           </LinearLayout>
       <LinearLayout
       android:id="@+id/bt3"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
        android:orientation="vertical"
       >                  
           </LinearLayout> 
   
   </LinearLayout>
        
</LinearLayout>  --------------------编程问答-------------------- public class MainActivity extends Activity {
//private LinearLayout llbtDataConfig;
 private LinearLayout llbtDataConfig1 = null;  //main布局中包裹本按钮的容器
 private LinearLayout llbtDataConfig2 = null;  //main布局中包裹本按钮的容器
 private LinearLayout llbtDataConfig3 = null;
 private LinearLayout line1 = null;
 
  private MyImageButton btDataConfig1 = null;
  private MyImageButton btDataConfig2 = null;
  private MyImageButton btDataConfig3 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        //语言国际化
        String localeLan = "";
        localeLan = Locale.getDefault().getLanguage();
        Locale local1 = new Locale(localeLan);      
        Locale.setDefault(local1);
        Configuration config = new Configuration();
        config.locale = local1;
        getBaseContext().getResources().updateConfiguration(config, null);

        
        line1 = (LinearLayout)findViewById(R.id.line1);
        //----画自定义按钮1
        btDataConfig1 = new MyImageButton(this, R.drawable.medical, R.string.str_btn); 
       //获取包裹本按钮的容器 
        llbtDataConfig1 = (LinearLayout) findViewById(R.id.bt1); 

        //将我们自定义的Button添加进这个容器 
        llbtDataConfig1.addView(btDataConfig1);

       // btDataConfig1.setWeightSum(1);
        
        //设置按钮的监听
         btDataConfig1.setOnClickListener(new Button.OnClickListener() { 
          public void onClick(View v) { 
            // btDataConfig1.setText("按钮被点   击过了");
             btDataConfig1.setBackgroundColor(Color.argb(100, 220, 200, 150));}               
          }); 
        //----画自定义按钮2
         btDataConfig2 = new MyImageButton(this, R.drawable.medical, R.string.str_btn); 
        //获取包裹本按钮的容器 
         llbtDataConfig2 = (LinearLayout) findViewById(R.id.bt2); 

         // llbtDataConfig2.setLayoutParams(lp);
         //llbtDataConfig2.setBackgroundColor(Color.argb(55, 250, 200, 150));
         //将我们自定义的Button添加进这个容器 
         llbtDataConfig2.addView(btDataConfig2); 

        /* llbtDataConfig2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                 LinearLayout.LayoutParams.FILL_PARENT,
                 1));   */        
         //设置按钮的监听
          btDataConfig2.setOnClickListener(new Button.OnClickListener() { 
           public void onClick(View v) { 
              //btDataConfig2.setText("按钮被点   击过了");
            }               
           });   
          
         } 
}
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,