自定义控件-左图右字

很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法。

<?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>

这个xml实现一个左图右字的布局,接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:
import android.content.Context;
04.import android.util.AttributeSet;
05.import android.view.LayoutInflater;
06.import android.widget.ImageView;
07.import android.widget.LinearLayout;
08.import android.widget.TextView;
09.
10.public class ImageBt extends LinearLayout {
11.
12. private ImageView iv;
13. private TextView tv;
14.
15. public ImageBt(Context context) {
16. this(context, null);
17. }
18.
19. public ImageBt(Context context, AttributeSet attrs) {
20. super(context, attrs);
21. // 导入布局
22. LayoutInflater.from(context).inflate(R.layout.custombt, this, true);
23. iv = (ImageView) findViewById(R.id.iv);
24. tv = (TextView) findViewById(R.id.tv);
25.
26. }
27.
28. /**
29. * 设置图片资源
30. */
31. public void setImageResource(int resId) {
32. iv.setImageResource(resId);
33. }
34.
35. /**
36. * 设置显示的文字
37. */
38. public void setTextViewText(String text) {
39. tv.setText(text);
40. }
41.
42.}

第三步,在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。方法如下:
01.<RelativeLayout
02. android:orientation="horizontal"
03. android:layout_width="fill_parent"
04. android:layout_height="wrap_content"
05. android:layout_gravity="bottom"
06. >
07. <com.notice.ib.ImageBt
08. android:id="@+id/bt_confirm"
09. android:layout_height="wrap_content"
10. android:layout_width="wrap_content"
11. android:layout_alignParentBottom="true"
12. android:background="@drawable/btbg"
13. android:clickable="true"
14. android:focusable="true"
15. />
16. <com.notice.ib.ImageBt
17. android:id="@+id/bt_cancel"
18. android:layout_toRightOf="@id/bt_confirm"
19. android:layout_height="wrap_content"
20. android:layout_width="wrap_content"
21. android:layout_alignParentBottom="true"
22. android:background="@drawable/btbg"
23. android:clickable="true"
24. android:focusable="true"
25. />
26. </RelativeLayout>

注意的是,控件标签使用完整的类名即可。为了给按钮一个点击效果,你需要给他一个selector背景,这里就不说了。

最后一步,即在activity中设置该控件的内容。当然,在xml中也可以设置,但是只能设置一个,当我们需要两次使用这样的控件,并且显示内容不同时就不行了。在activity中设置也非常简单,我们在ImageBt这个类中已经写好了相应的方法,简单调用即可。代码如下:
01.public class MainActivity extends Activity {
02.
03. private ImageBt ib1;
04. private ImageBt ib2;
05.
06. /** Called when the activity is first created. */
07. @Override
08. public void onCreate(Bundle savedInstanceState) {
09. super.onCreate(savedInstanceState);
10. setContentView(R.layout.login);
11.
12. ib1 = (ImageBt) findViewById(R.id.bt_confirm);
13. ib2 = (ImageBt) findViewById(R.id.bt_cancel);
14.
15. ib1.setTextViewText("确定");
16. ib1.setImageResource(R.drawable.confirm);
17. ib2.setTextViewText("取消");
18. ib2.setImageResource(R.drawable.cancel);
19.
20. ib1.setOnClickListener(new OnClickListener() {
21.
22. @Override
23. public void onClick(View v) {
24. //在这里可以实现点击事件
25. }
26. });
27.
28. }
29.}

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值