您的位置:首页 > 其它

自定义标题栏(中间是三个文本)

2015-10-31 18:48 218 查看
* 在之前写的那篇文章的基础上再增加一些内容,就是中间有三个文本,可点击 *

首先增加以下属性:

-

<!-- 中间三个文本的默认颜色和按下颜色-->
<attr name="centerTextDefaultColor" format="color"></attr>
<attr name="centerTextPressColor" format="color"></attr>`

<attr name="centerText1" format="string"></attr>
<attr name="centerText2" format="string"></attr>
<attr name="centerText3" format="string"></attr>


取出属性,把这些属性设置到中间三个文本

把三个文本添加到中间线性布局

把中间线性布局添加到总布局

设置三个文本的监听事件

全部代码

package com.konglinghuashi.taqu.costomview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.konglinghuashi.taqu.R;

/**
* Created by KongLingHuaShi on 2015/10/13.
*/
public class CommonToolbar extends RelativeLayout implements View.OnClickListener {
/**
* 包含图片和文本的左右两个线性布局
*/
LinearLayout leftLayout;
LinearLayout rightLayout;

/**
* 包含文本的中间布局
*/
LinearLayout centerLayout;

/**
* 左右两个图片
*/
Drawable leftImage;
Drawable rightImage;

/**
* 左右和中间的文字
*/
String leftText;
String centerText1;
String centerText2;
String centerText3;
String rightText;

/**
* 文字的大小和颜色
*/
float textSize;
int textColor;

/**
* 中间三个文本的默认颜色和按下颜色
*/
int centerTextDefaultColor;
int centerTextPressColor;

/***
* 左边图片,右边图片,左边文字,中间文字,右边文字五个控件
*/
ImageView leftIV;
ImageView rightIV;
TextView leftTV;
TextView centerTV1;
TextView centerTV2;
TextView centerTV3;
TextView rightTV;

/**
* 控制左中右三个部分的参数,用来设置布局里面的控件的宽高和位置
*/
RelativeLayout.LayoutParams leftParams;
RelativeLayout.LayoutParams centerParams;
RelativeLayout.LayoutParams rightParams;

/**
* 左右两个线性布局分别距离左边距和右边距的距离
*/
float leftLayoutPading;
float rightLayoutPading;

/**
* 左右两个线性布局的监听接口
*/
ILeftLayoutClickListener leftListener;
IRightLayoutClickListener rightListener;

/**
* 中间布局的监听接口
*/
ICenterLayoutClickListener centerListener;

public CommonToolbar(Context context, AttributeSet attrs) {
super(context, attrs);

//TypedArray用于取出写在values目录下的attr文件下的styleable属性
TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.CommonToolbar);

/**
* 逐个取出
*/
leftImage = t.getDrawable(R.styleable.CommonToolbar_leftImage);
rightImage = t.getDrawable(R.styleable.CommonToolbar_rightImage);

leftText = t.getString(R.styleable.CommonToolbar_leftText);
centerText1 = t.getString(R.styleable.CommonToolbar_centerText1);
centerText2 = t.getString(R.styleable.CommonToolbar_centerText2);
centerText3 = t.getString(R.styleable.CommonToolbar_centerText3);
rightText = t.getString(R.styleable.CommonToolbar_rigtText);

//这里获取的是像素,不知道怎么样可以再控件那边写sp或者dp,获取的也是sp或dp
textSize = t.getDimensionPixelSize(R.styleable.CommonToolbar_textSize, 20);

//获取文本颜色
textColor = t.getColor(R.styleable.CommonToolbar_textColor, Color.WHITE);
centerTextDefaultColor = t.getColor(R.styleable.CommonToolbar_centerTextDefaultColor,
Color.WHITE);
centerTextPressColor = t.getColor(R.styleable.CommonToolbar_centerTextPressColor,
Color.BLACK);

//获取左右线性布局的内边距
leftLayoutPading = t.getDimension(R.styleable.CommonToolbar_leftLayoutPading, 10);
rightLayoutPading = t.getDimension(R.styleable.CommonToolbar_rightLayoutPading, 10);

/**
* 回收TypedArray
*/
t.recycle();

/**
* 实例化子控件
*/
leftIV = new ImageView(context);
rightIV = new ImageView(context);

leftTV = new TextView(context);
centerTV1 = new TextView(context);
centerTV2 = new TextView(context);
centerTV3 = new TextView(context);
rightTV = new TextView(context);

leftLayout = new LinearLayout(context);
rightLayout = new LinearLayout(context);
centerLayout = new LinearLayout(context);

/**
* 把取出的属性添加到对应的子控件上
*/
leftIV.setImageDrawable(leftImage);
rightIV.setImageDrawable(rightImage);

leftTV.setText(leftText);
centerTV1.setText(centerText1);
centerTV2.setText(centerText2);
centerTV3.setText(centerText3);
rightTV.setText(rightText);

leftTV.setTextSize(textSize);
centerTV1.setTextSize(textSize);
centerTV2.setTextSize(textSize);
centerTV3.setTextSize(textSize);
rightTV.setTextSize(textSize);

leftTV.setTextColor(textColor);
//这里之所以设置centerTV1的字体颜色是按下状态的颜色,是因为想让程序一进入这个界面就是选中centerTV1的状态
centerTV1.setTextColor(centerTextPressColor);
centerTV2.setTextColor(centerTextDefaultColor);
centerTV3.setTextColor(centerTextDefaultColor);
rightTV.setTextColor(textColor);

//设置中间的文本居中
centerTV1.setGravity(Gravity.CENTER);
centerTV2.setGravity(Gravity.CENTER);
centerTV2.setPadding(20, 0, 20, 0);
centerTV3.setGravity(Gravity.CENTER);

//设置左边线性布局垂直居中对齐
leftLayout.setGravity(Gravity.CENTER_VERTICAL);
//设置左边线性布局距离左边距的距离,这样做之后会和屏幕形成一定边距,比较美观,但是这时点击段空白区域是没反应的
//因为线性布局不包括这里,如果又要美观又要点这个区域有反应,那只能是设置左边线性布局里面的这第一个控件的边距,而不设置
//线性布局的边距,这样就可以达到效果
leftLayout.setPadding((int) leftLayoutPading, 0, 0, 0);
//leftIV.setPadding((int) leftLayoutPading, 0, 0, 0);

//把左边的Imageview和textview添加到左边线性布局
leftLayout.addView(leftIV);
leftLayout.addView(leftTV);

//把左边线性布局在总布局(整个Toolbar)的大小为WRAP_CONTENT
leftParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//把左边线性布局在总布局(整个Toolbar)的左边,并且垂直居中
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
leftParams.addRule(RelativeLayout.CENTER_VERTICAL);
//把左边线性布局按照leftParams的规则添加到总布局中
addView(leftLayout, leftParams);

//设置中间线性布局里面的控件在它那里的布局是垂直居中
centerLayout.setGravity(Gravity.CENTER_VERTICAL);

//把三个文本添加到中间线性布局
centerLayout.addView(centerTV1);
centerLayout.addView(centerTV2);
centerLayout.addView(centerTV3);
//中间线性布局添加到添加到总布局
centerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
centerParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(centerLayout, centerParams);

//右边线性布局添加到总布局,和左边线性布局一样
rightLayout.setGravity(Gravity.CENTER_VERTICAL);
setPadding(0, 0, (int) rightLayoutPading, 0);
rightLayout.addView(rightTV);
rightLayout.addView(rightIV);

rightParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rightParams.addRule(RelativeLayout.CENTER_VERTICAL);
addView(rightLayout, rightParams);

/**
* 中间文本设置监听
*/
centerTV1.setOnClickListener(this);
centerTV2.setOnClickListener(this);
centerTV3.setOnClickListener(this);

}

@Override
public void onClick(View v) {

//左边线性布局监听
if (v == leftLayout) {
if (leftListener != null) {
leftListener.leftLayoutClick();
}
}
//右边线性布局监听
if (v == rightLayout) {
if (rightListener != null) {
rightListener.rightLayoutClick();
}
}
//中间布局第一个文本监听
if (v == centerTV1) {
centerTV2.setTextColor(centerTextDefaultColor);
centerTV3.setTextColor(centerTextDefaultColor);
centerTV1.setTextColor(centerTextPressColor);
if (centerListener != null) {
centerListener.centerTV1();
}
}
//中间布局第二个文本监听
if (v == centerTV2) {
centerTV1.setTextColor(centerTextDefaultColor);
centerTV3.setTextColor(centerTextDefaultColor);
centerTV2.setTextColor(centerTextPressColor);
if (centerListener != null) {
centerListener.centerTV2();
}
}
//中间布局第三个文本监听
if (v == centerTV3) {
centerTV1.setTextColor(centerTextDefaultColor);
centerTV2.setTextColor(centerTextDefaultColor);
centerTV3.setTextColor(centerTextPressColor);
if (centerListener != null) {
centerListener.centerTV3();
}
}
}

/***
* 左边布局监听接口
*/
public interface ILeftLayoutClickListener {
public void leftLayoutClick();
}

/**
* 右边布局监听接口
*/
public interface IRightLayoutClickListener {
public void rightLayoutClick();
}

/**
* 右边布局监听接口
*/
public interface ICenterLayoutClickListener {
public void centerTV1();

public void centerTV2();

public void centerTV3();
}

/**
* 给用户调用的左边布局监听接口
*
* @param listener 设置左边布局接口
*/
public void setOnLeftLayoutClickListener(ILeftLayoutClickListener listener) {

this.leftListener = listener;

}

/**
* 给用户调用的右边布局监听接口
*
* @param listener 设置右边布局接口
*/
public void setOnRightLayoutClickListener(IRightLayoutClickListener listener) {
this.rightListener = listener;
}

/**
* 设置中间布局监听接口
*
* @param centerListener 设置中间布局接口
*/
public void setOnCenterLayoutClickListener(ICenterLayoutClickListener centerListener) {
this.centerListener = centerListener;
}

public void setPress() {
centerTV1.setEnabled(true);
// centerTV1.setPressed(true);
}
}


使用

<com.konglinghuashi.taqu.costomview.CommonToolbar
android:id="@+id/commonToolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/basecolor"
costom:textSize="20px"
costom:centerTextDefaultColor="#999999"
costom:centerTextPressColor="#353535"
costom:centerText1="全部"
costom:centerText2="新鲜"
costom:centerText3="精华"
costom:leftImage="@mipmap/left_imageview_back_black"
costom:leftLayoutPading="10dp"
costom:leftText="返回"
costom:textColor="@color/black"
/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: