设为首页 加入收藏

TOP

Android UI开发详解之模板控件的复用(一)
2014-11-24 08:05:36 来源: 作者: 【 】 浏览:2
Tags:Android 开发 详解 模板 控件 复用

如图:



我们很多程序的界面中,顶部的TopBar是不变的,所以,我们可以做一个公用的控件模板,每次使用时,只要设置相应的参数,就能生成这样一个TopBar。


模板控件实现方法:


package com.xys.multiplexedmodule;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MultipleTopBar extends RelativeLayout{

private Button btn_left;
private Button btn_right;
private TextView tv_title;

private TopBarClickListener topBarClickListener;
private String str_title;

private RelativeLayout.LayoutParams leftButtonLayoutParams;
private RelativeLayout.LayoutParams rightButtonLayoutParams;
private RelativeLayout.LayoutParams tvTitleLayoutParams;

private static int leftBtnId=1;
private static int titleTvId=2;
private static int rightBtnId=3;

private Drawable leftBtnBackground;
private Drawable rightBtnBackground;

private String str_LeftBtn;
private String str_RightBtn;
private int leftBtnColor;
private int rightBtnColor;
private int titleTvColor;

private float titleTextSize;

public MultipleTopBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//从参数列表中获取参数
//TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合
//第二个参数为 为获取到值时的默认值
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.TopBar);
this.str_title=ta.getString(R.styleable.TopBar_title);
this.leftBtnBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
this.rightBtnBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
this.str_LeftBtn=ta.getString(R.styleable.TopBar_leftText);
this.str_RightBtn=ta.getString(R.styleable.TopBar_rightText);
this.leftBtnColor=ta.getColor(R.styleable.TopBar_leftTextColor, 0);
this.rightBtnColor=ta.getColor(R.styleable.TopBar_rightTextColor, 0);
this.titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize, 14);
this.titleTvColor=ta.getColor(R.styleable.TopBar_titleTextColor, 0);

ta.recycle();

btn_left=new Button(context);
btn_right=new Button(context);
tv_title=new TextView(context);

btn_left.setId(leftBtnId);
btn_right.setId(rightBtnId);
tv_title.setId(titleTvId);

//为组件配置布局参数
leftButtonLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rightButtonLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
tvTitleLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

leftButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
leftButtonLayoutParams.setMargins(12, 0, 0, 0);//左上右下
leftButtonLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

rightButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
rightButtonLayo

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 通知PendingIntent意图打.. 下一篇Android系统详解之获取图片和视频..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)