之前做的东西看上去没什么用,但是知识还是挺有用的.下面我们就来打造一款Topbar吧,如果前面的几篇文章看过的话,应该也就能看懂这个.^_^,附上效果图
1.attrs属性文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="backgroundColor" format="color" />
<attr name="textColor" format="color" />
<attr name="titleText" format="string" />
<attr name="titleTextSize" format="dimension" />
<attr name="leftText" format="string" />
<attr name="leftTextSize" format="dimension" />
<attr name="rightText" format="string" />
<attr name="rightTextSize" format="dimension" />
<declare-styleable name="Topbar">
<attr name="backgroundColor" />
<attr name="textColor" />
<attr name="titleText" />
<attr name="titleTextSize" />
<attr name="leftText" />
<attr name="leftTextSize" />
<attr name="rightText" />
<attr name="rightTextSize" />
</declare-styleable>
</resources>
2.整体View的代码
package com.example.august.customview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by August on 16/4/9.
*/
public class Topbar extends RelativeLayout {
private int backgroundColor;
private int textColor;
private int defaultTextSize;
/**
* 控件及其对应的属性
*/
private TextView leftTextView;
private String leftText;
private int leftTextSize;
private TextView titleTextView;
private String titleText;
private int titleTextSize;
private TextView rightTextView;
private String rightText;
private int rightTextSize;
private OnTopBarClickListener mOnTopBarClickListener; //我们自己的点击监听回调
private OnClickListener mLeftOnClickListener; //内部控件的三个点击监听回调
private OnClickListener mTitleOnClickListener;
private OnClickListener mRightOnClickListener;
public Topbar(Context context) {
this(context, null);
}
public Topbar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Topbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Topbar, defStyleAttr, 0);
defaultTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,12,getResources().getDisplayMetrics());
int n = ta.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = ta.getIndex(i);
switch (attr) {
case R.styleable.Topbar_backgroundColor:
backgroundColor = ta.getColor(attr, 0);
break;
case R.styleable.Topbar_textColor:
textColor = ta.getColor(attr, Color.WHITE);
break;
case R.styleable.Topbar_leftText:
leftText = ta.getString(attr);
break;
case R.styleable.Topbar_leftTextSize:
leftTextSize = (int) ta.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16
, getResources().getDisplayMetrics()));
break;
case R.styleable.Topbar_rightText:
rightText = ta.getString(attr);
break;
case R.styleable.Topbar_rightTextSize:
rightTextSize = (int) ta.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16
, getResources().getDisplayMetrics()));
break;
case R.styleable.Topbar_titleText:
titleText = ta.getString(attr);
break;
case R.styleable.Topbar_titleTextSize:
titleTextSize = (int) ta.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16
, getResources().getDisplayMetrics()));
break;
}
}
ta.recycle();
rightTextSize = rightTextSize == 0 ? rightTextSize : defaultTextSize;
titleTextSize = titleTextSize == 0 ? titleTextSize : defaultTextSize;
leftTextSize = leftTextSize == 0 ? leftTextSize : defaultTextSize;
leftTextView = new TextView(context);
titleTextView = new TextView(context);
rightTextView = new TextView(context);
leftTextView.setText(leftText);
leftTextView.setTextSize(leftTextSize);
titleTextView.setText(titleText);
titleTextView.setTextSize(titleTextSize);
rightTextView.setText(rightText);
rightTextView.setTextSize(rightTextSize);
leftTextView.setTextColor(textColor);
titleTextView.setTextColor(textColor);
rightTextView.setTextColor(textColor);
setBackgroundColor(backgroundColor);
setGravity(CENTER_VERTICAL);
LayoutParams leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(ALIGN_PARENT_LEFT);
LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
titleParams.addRule(CENTER_IN_PARENT);
LayoutParams rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(ALIGN_PARENT_RIGHT);
initListener();
addView(leftTextView, leftParams);
addView(titleTextView, titleParams);
addView(rightTextView, rightParams);
}
private void initListener() {
mLeftOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnTopBarClickListener != null) {
mOnTopBarClickListener.onLeftClick(v);
}
}
};
mTitleOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnTopBarClickListener != null) {
mOnTopBarClickListener.onTitleClick(v);
}
}
};
mRightOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnTopBarClickListener != null) {
mOnTopBarClickListener.onRightClick(v);
}
}
};
leftTextView.setOnClickListener(mLeftOnClickListener);
titleTextView.setOnClickListener(mTitleOnClickListener);
rightTextView.setOnClickListener(mRightOnClickListener);
}
/**
* 我们自己的监听回调
*/
public interface OnTopBarClickListener {
void onRightClick(View v);
void onTitleClick(View v);
void onLeftClick(View v);
}
/**
* 对外公布一个设置监听回调的方法
* @param listener
*/
public void setOnTopBarClickListener(OnTopBarClickListener listener) {
mOnTopBarClickListener = listener;
}
/**
* 设置三个控件是否可见
* @param isVisible
*/
public void setRightVisibility(boolean isVisible) {
if (isVisible) {
rightTextView.setVisibility(View.VISIBLE);
} else {
rightTextView.setVisibility(View.GONE);
}
}
public void setTitleVisibility(boolean isVisible) {
if (isVisible) {
titleTextView.setVisibility(View.VISIBLE);
} else {
titleTextView.setVisibility(View.GONE);
}
}
public void setLeftVisibility(boolean isVisible) {
if (isVisible) {
leftTextView.setVisibility(View.VISIBLE);
} else {
leftTextView.setVisibility(View.GONE);
}
}
}
3.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.august.customview.Topbar
android:id="@+id/mTopbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
app:backgroundColor="#f84254"
app:leftText="返回"
app:leftTextSize="12sp"
app:rightText="修改"
app:rightTextSize="12sp"
app:textColor="#FFFFFF"
app:titleText="个人信息"
app:titleTextSize="12sp"></com.example.august.customview.Topbar>
</RelativeLayout>
4.MainActivity代码:
package com.example.august.customview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private Topbar mTopbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
mTopbar = (Topbar) findViewById(R.id.mTopbar);
mTopbar.setOnTopBarClickListener(new Topbar.OnTopBarClickListener() {
@Override
public void onRightClick(View v) {
Toast.makeText(MainActivity.this, "Right Clicked!", Toast.LENGTH_SHORT).show();
}
@Override
public void onTitleClick(View v) {
Toast.makeText(MainActivity.this, "Title Clicked!", Toast.LENGTH_SHORT).show();
}
@Override
public void onLeftClick(View v) {
Toast.makeText(MainActivity.this, "Left Clicked!", Toast.LENGTH_SHORT).show();
}
});
mTopbar.setRightVisibility(false);
}
}
可以看到,自定义View的功能可是非常的强大的,大家可以根据自己的需要来封装各种View.