Andriod自定义按钮

本文详细介绍了如何创建一个包含图片和文本的自定义控件,通过继承LinearLayout并自定义方法来实现按钮的布局和交互。包括定义布局、创建自定义类、在XML中使用控件以及在Activity中设置内容的全过程。

作者:陈水宝

效果如图所示:(先设计好图片)



第一步,定义一个layout,实现按钮内部的布局。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv"
    android:src="@drawable/confirm"
    android:paddingTop="5dip"
    android:paddingBottom="5dip"
    android:paddingLeft="40dip"
    android:layout_gravity="center_vertical"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="确定"
    android:textColor="#000000"
    android:id="@+id/tv"
    android:layout_marginLeft="8dip"
    android:layout_gravity="center_vertical"
    />
</LinearLayout>

第三步,在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。方法如下:


<RelativeLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         >
         <com.notice.ib.ImageBt
             android:id="@+id/bt_confirm"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:layout_alignParentBottom="true"
             android:background="@drawable/btbg"
             android:clickable="true"
             android:focusable="true"
             />
         <com.notice.ib.ImageBt
             android:id="@+id/bt_cancel"
             android:layout_toRightOf="@id/bt_confirm"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:layout_alignParentBottom="true"
             android:background="@drawable/btbg"
             android:clickable="true"
             android:focusable="true"
            />
         </RelativeLayout>




第二步,写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:


package com.notice.ib;
  
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
  
public class ImageBt extends LinearLayout {
  
    private ImageView iv;
    private TextView  tv;
  
    public ImageBt(Context context) {
        this(context, null);
    }
  
    public ImageBt(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 导入布局
        LayoutInflater.from(context).inflate(R.layout.custombt, this, true);
        iv = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv);
  
    }
  
    /**
     * 设置图片资源
     */
    public void setImageResource(int resId) {
        iv.setImageResource(resId);
    }
  
    /**
     * 设置显示的文字
     */
    public void setTextViewText(String text) {
        tv.setText(text);
    }
  
}


第四步,即在 activity中设置该控件的内容。当然,在xml中也可以设置,但是只能设置一个,当我们需要两次使用这样的控件,并且显示内容不 同时就不行了。在activity中设置也非常简单,我们在ImageBt这个类中已经写好了相应的方法,简单调用即可。代码如下:


public class MainActivity extends Activity {
  
    private ImageBt ib1;
    private ImageBt ib2;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
  
        ib1 = (ImageBt) findViewById(R.id.bt_confirm);
        ib2 = (ImageBt) findViewById(R.id.bt_cancel);
  
        ib1.setTextViewText("确定");
        ib1.setImageResource(R.drawable.confirm);
        ib2.setTextViewText("取消");
        ib2.setImageResource(R.drawable.cancel);
  
        ib1.setOnClickListener(new OnClickListener() {
  
            @Override
            public void onClick(View v) {
                    //在这里可以实现点击事件
            }
        });
  
    }
}


这样,一个带文字和图片的组合按钮控件就完成了。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值