是这样的,今天打算做一个自定义控件,导航栏。去年这个时候做过,然后忘记了,后面怎么百度都找不到我参考的那篇自定义的博文了,所以我就根据自己的印象写了一个,如果有些雷同,说明我是参考您的博文重新整理了一遍哦~~这篇文章不只是针对导航栏 ,我只是用导航栏举个例子,其他需要的自定义控件也是大同小异的,如果有问题,还请多多指教。
现在开始 自定义控件--->首先:在value文件中新建一个atts.xml,如果有就直接添加下面的代码:
<declare-styleable name="TopBar">
<!-- 中间标题文字-->
<attr name="title" format="string"/>
<!-- 中间文字大小-->
<attr name="titleTextSize" format="dimension"/>
<!-- 中间文字颜色-->
<attr name="titleTextColor" format="color"/>
<!-- 左边图片-->
<attr name="leftImage" format="reference"/>
<!-- 左边背景-->
<attr name="leftBackground" format="reference|color"/>
<!-- 右边按钮文本颜色-->
<attr name="rightTextColor" format="color"/>
<!-- 右边按钮文本大小-->
<attr name="rightTextSize" format="dimension"/>
<!-- 右边按钮的背景-->
<attr name="rightBackground" format="reference|color"/>
<!-- 右边按钮的文字-->
<attr name="rightText" format="string"/>
</declare-styleable>
<declare-styleable name="TopBar">
<!-- 中间标题文字-->
<attr name="title" format="string"/>
<!-- 中间文字大小-->
<attr name="titleTextSize" format="dimension"/>
<!-- 中间文字颜色-->
<attr name="titleTextColor" format="color"/>
<!-- 左边图片-->
<attr name="leftImage" format="reference"/>
<!-- 左边背景-->
<attr name="leftBackground" format="reference|color"/>
<!-- 右边按钮文本颜色-->
<attr name="rightTextColor" format="color"/>
<!-- 右边按钮文本大小-->
<attr name="rightTextSize" format="dimension"/>
<!-- 右边按钮的背景-->
<attr name="rightBackground" format="reference|color"/>
<!-- 右边按钮的文字-->
<attr name="rightText" format="string"/>
</declare-styleable>
无论自定义啥,都需要先把要修改的属性写到atts.xml就像 android:layout_width="25dp"android:layout_height="25dp" 的属性是一样的。
然后--->
public class MyTopBar extends RelativeLayout {
private ImageButton leftButton;//左边返回箭头
private TextView tvTitle;//中间标题
private TextView rightButton;//右边其它选择
private topbarClickListener listener;
/**
* 设置左右两边的监听事件接口
*/
public interface topbarClickListener{
public void leftClick();
public void rightClick();
}
public void setOnTopbarClickListener(topbarClickListener listener){
this.listener=listener;
}
//三个控件的布局设置
private LayoutParams leftParams,rightParams,titleParams;
// 标题
private int textColor;//字体颜色
private float titleTextSize;//字体大小
private String title; //标题
// 左边
private Drawable leftBackground;//左边背景图
private Drawable leftImage;//左边图
// 右边
private int rightTextColor;//右字体颜色
private String rightText;//右边文字
private float rightTextSize;//右边文字大小
private Drawable rightBackground;//右边背景
// 将attr对应 构造函数
public MyTopBar(final Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
//将 左、中、右 在atts.xml的属性一一对应
textColor=ta.getInt(R.styleable.TopBar_titleTextColor,0);//字体颜色
titleTextSize = ta.getDimensionPixelSize(R.styleable.TopBar_titleTextSize,0);//字体大小
title = ta.getString(R.styleable.TopBar_title);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);//左边图
leftImage = ta.getDrawable(R.styleable.TopBar_leftImage);
rightTextColor = ta.getInt(R.styleable.TopBar_rightTextColor,0);//右字体颜色
rightText = ta.getString(R.styleable.TopBar_rightText);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
rightTextSize = ta.getDimensionPixelSize(R.styleable.TopBar_rightTextSize,0);
ta.recycle();
//实例化左中右控件
leftButton = new ImageButton(context);
tvTitle = new TextView(context);
rightButton = new TextView(context);
//将左、中、右控件与数学一一对应起来
leftButton.setBackgroundDrawable(leftBackground);
leftButton.setImageDrawable(leftImage);
tvTitle.setText(title);
tvTitle.setTextColor(textColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setTextSize(rightTextSize);
rightButton.setBackgroundColor(Color.parseColor("#66ccff"));
rightButton.setVisibility(View.GONE);//默认右边的文字隐藏
rightButton.setGravity(CENTER_HORIZONTAL);
setBackgroundColor(Color.parseColor("#66ccff"));
// setBackgroundColor(Color.parseColor("#ff0000"));
leftParams = new LayoutParams(120,140);//宽、高;
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
// leftParams.setMargins(50,10,0,10);
addView(leftButton,leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rightParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
rightParams.setMargins(0,0,10,0);
addView(rightButton,rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
addView(tvTitle,titleParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
// getActivity(getActivity).finish();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
}
public void setRightIsVisible(boolean flag){
if(flag){
rightButton.setVisibility(View.VISIBLE);
}else{
rightButton.setVisibility(View.GONE);
}
}
}
这样自定义导航栏就做好了,
在xml上使用即可
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto" //这句一定要加
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_bg">
<!--ml 为自定义内容-->
<com.example.ma.mali03.MyView.MyTopBar
android:id="@+id/mashang_topbar"
android:layout_width="match_parent"
android:layout_height="@dimen/x50"
custom:title="马上预约"
custom:titleTextSize = "19sp"
custom:textColor="#fff"
custom:leftImage="@drawable/all_back"
>
</com.example.ma.mali03.MyView.MyTopBar></LinearLayout>
最后在.javas上和普通控件一样使用就可以了。
myTopBar = (MyTopBar)findViewById(R.id.mashang_topbar);
myTopBar.setOnTopbarClickListener(new MyTopBar.topbarClickListener() {
@Override
public void leftClick() {
finish();
}
@Override
public void rightClick() {
}
});
等我想起git的密码后我会把完整的源代码放到github上