android自定义控件以及复用控件示例详解

1.png
2012-8-10 16:35 上传
下载附件 (154.82 KB)


对于上述几张图片,他们都有一个共同点,布局差不多,我们可以把它封装成一个共同的控件,通过自定义属性来控制子控件的显示与否与内容的不同,背景的不同

  1. package com.android.custom;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Color;
  5. import android.graphics.drawable.Drawable;
  6. import android.text.TextUtils.TruncateAt;
  7. import android.util.AttributeSet;
  8. import android.view.Gravity;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.RelativeLayout;
  13. import android.widget.TextView;
  14. public class TopBar extends RelativeLayout{
  15. private Button leftBtn,rightBtn;
  16. private TextView title;
  17. private TopBarClickListener topBarClickListener;
  18. private String titleStr;

  19. private RelativeLayout.LayoutParams leftBtnLayoutParams,rightBtnLayoutParams,titleLayoutParams;
  20. private static int LEFT_BTN_ID = 1;
  21. private static int TITLE_ID = 2;
  22. private static int RIGHT_BTN_ID = 3;

  23. private Drawable leftBackground,rightBackground;
  24. private String leftText,rightText;
  25. private int leftTextColor,rightTextColor,titleTextColor;
  26. private float titleTextSize;

  27. public TopBar(Context context,AttributeSet attrs){
  28.   super(context,attrs);
  29.  
  30.   TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
  31.  
  32.   this.titleStr = ta.getString(R.styleable.TopBar_title);
  33.   this.leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
  34.   this.rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
  35.   this.leftText = ta.getString(R.styleable.TopBar_leftText);
  36.   this.rightText = ta.getString(R.styleable.TopBar_rightText);
  37.   this.leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
  38.   this.rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
  39.   this.titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 14);
  40.   this.titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
  41.  
  42.   ta.recycle();
  43.  
  44.   leftBtn = new Button(context);
  45.   rightBtn = new Button(context);
  46.   title = new TextView(context);
  47.  
  48.   leftBtn.setId(LEFT_BTN_ID);
  49.   rightBtn.setId(RIGHT_BTN_ID);
  50.   title.setId(TITLE_ID);
  51.  
  52.  
  53.  
  54.   leftBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  55. rightBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  56.   titleLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  57.  
  58.   leftBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
  59.   leftBtnLayoutParams.setMargins(12, 0, 0, 0);//左上右下
  60.   leftBtnLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
  61.  
  62.   rightBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  63.   rightBtnLayoutParams.setMargins(12, 0, 0, 0);//左上右下
  64.   rightBtnLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
  65.  
  66.   titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  67.   titleLayoutParams.setMargins(0, 0, 12, 0);//左上右下
  68.   titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
  69.   titleLayoutParams.addRule(RelativeLayout.LEFT_OF, RIGHT_BTN_ID);
  70.   titleLayoutParams.addRule(RelativeLayout.RIGHT_OF, LEFT_BTN_ID);
  71.   titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
  72.  
  73.   addView(leftBtn, leftBtnLayoutParams);
  74.   addView(rightBtn,rightBtnLayoutParams);
  75.   addView(title,titleLayoutParams);
  76.  
  77.   leftBtn.setBackgroundDrawable(leftBackground);
  78.   leftBtn.setText(leftText);
  79.   leftBtn.setTextColor(leftTextColor);
  80.   rightBtn.setBackgroundDrawable(rightBackground);
  81.   rightBtn.setText(rightText);
  82.   rightBtn.setTextColor(rightTextColor);
  83.  
  84.   title.setTextSize(22.0f);
  85.   title.setTextColor(Color.BLUE);
  86.   title.setEllipsize(TruncateAt.MIDDLE);
  87.   title.setGravity(Gravity.CENTER_HORIZONTAL);
  88.   title.setSingleLine(true);
  89.   title.setText(titleStr);
  90.   title.setTextSize(titleTextSize);
  91.   title.setTextColor(titleTextColor);
  92.  
  93.   leftBtn.setOnClickListener(new OnClickListener() {
  94.    @Override
  95.    public void onClick(View v) {
  96.     if(topBarClickListener!=null){
  97.      topBarClickListener.leftBtnClick();
  98.     }
  99.    }
  100.   });
  101.  
  102.   rightBtn.setOnClickListener(new OnClickListener() {
  103.    @Override
  104.    public void onClick(View v) {
  105.     if(topBarClickListener!=null){
  106.      topBarClickListener.rightBtnClick();
  107.     }
  108.    }
  109.   });
  110. }
  111. public void setTopBarClickListener(TopBarClickListener topBarClickListener) {
  112.   this.topBarClickListener = topBarClickListener;
  113. }
  114. }

复制代码

  1. package com.android.custom;
  2. public interface TopBarClickListener {
  3. void leftBtnClick();
  4. void rightBtnClick();
  5. }

复制代码

  1. package com.android.custom;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Toast;
  5. public class CustomViewActivity extends Activity{
  6. private TopBar topBar;
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.main);
  11.        
  12.         topBar = (TopBar)findViewById(R.id.topBar);
  13.         topBar.setTopBarClickListener(new TopBarClickListener() {
  14.    @Override
  15.    public void rightBtnClick() {
  16.     //处理右边按钮所对应的逻辑
  17.     Toast.makeText(CustomViewActivity.this, "你点击的是右边的按钮", Toast.LENGTH_LONG).show();
  18.    }
  19.   
  20.    @Override
  21.    public void leftBtnClick() {
  22.     //处理左边按钮所对应的逻辑
  23.     Toast.makeText(CustomViewActivity.this, "你点击的是左边的按钮", Toast.LENGTH_LONG).show();
  24.    }
  25.   });
  26.     }
  27. }
复制代码
在res/xml文件夹下面新建attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="TopBar">
  4.   <attr name="title" format="string"/>
  5.   <attr name="titleTextSize" format="dimension"/>
  6.   <attr name="titleTextColor" format="color"/>
  7.   <attr name="leftTextColor" format="color"/>
  8.   <attr name="leftBackground" format="string"/>
  9.   <attr name="leftText" format="string"/>
  10.   <attr name="rightTextColor" format="color"/>
  11.   <attr name="rightBackground" format="string"/>
  12.   <attr name="rightText" format="string"/>
  13. </declare-styleable>
  14. </resources>
复制代码
布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res/com.android.custom"
  4. android:orientation="vertical" android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView android:layout_width="fill_parent"
  7.   android:layout_height="fill_parent" android:background="#ff00ff00" />
  8. <com.android.custom.TopBar android:id="@+id/topBar"
  9.   custom:title="自定义标题" custom:titleTextSize="14.0sp" custom:titleTextColor="#ff0000" android:layout_width="fill_parent"
  10.   custom:leftBackground="@drawable/icon" custom:rightBackground="@drawable/icon"
  11.   custom:leftText="左侧" custom:rightText="右侧"
  12.   custom:leftTextColor="#123123" custom:rightTextColor="#ff0000"
  13.   android:layout_height="wrap_content" android:background="#0000ff">
  14. </com.android.custom.TopBar>
  15. </FrameLayout>

复制代码
45ec687a-855e-3ecd-bfc8-ad8a1fa2059b.png
2012-8-10 16:39 上传
下载附件 (22.8 KB)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值