原文网址:点击打开链接
此文在原作者的基础上加了些注释,也减少了一些解释,做了些修改。如果看不太明白的朋友,可以参考原文
1.首先自定义控件属性
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="leftText" format="string" />
<attr name="leftTextColor" format="color" />
<attr name="leftTextSize" format="dimension" />
<attr name="leftTextBackground" format="reference|color" />
<attr name="centreText" format="string" />
<attr name="centreTextColor" format="color" />
<attr name="centreTextSize" format="dimension" />
<attr name="centreTextBackground" format="reference|color" />
<attr name="rightText" format="string" />
<attr name="rightTextColor" format="color" />
<attr name="rightTextSize" format="dimension" />
<attr name="rightTextBackground" format="reference|color" />
</declare-styleable>
</resources></span>
2
.自定义view,获取自定义view的属性,然后添加点击事件和给其内的view赋值
<span style="font-size:14px;">package zdd.huangfusheng.topbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import zdd.huangfusheng.R;
/**
* Created by ZDD on 2016/7/26.
* 自定义头
*/
public class TopBarView extends RelativeLayout {
// 头部文件的子控件
private TextView leftTextView, centreTextView, rightTextView;
// 子控件的位置
private LayoutParams leftParams, centreParams, rightParams;
// 左右的点击事件
private TopBarListener listener;
// 左边的数据
private String leftText;
private float leftTextSize;
private int leftTextColor;
private Drawable leftTextBackground;
// 中间的数据
private String centreText;
private float centreTextSize;
private int centreTextColor;
private Drawable centreTextBackground;
// 右边的数据
private String rightText;
private float rightTextSize;
private int rightTextColor;
private Drawable rightTextBackground;
public TopBarView(Context context) {
super(context);
}
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
leftText = typedArray.getString(R.styleable.TopBar_leftText);
leftTextSize = typedArray.getDimension(R.styleable.TopBar_leftTextSize, 0);
leftTextColor = typedArray.getColor(R.styleable.TopBar_leftTextColor, 0);
leftTextBackground = typedArray.getDrawable(R.styleable.TopBar_leftTextBackground);
centreText = typedArray.getString(R.styleable.TopBar_centreText);
centreTextSize = typedArray.getDimension(R.styleable.TopBar_centreTextSize, 0);
centreTextColor = typedArray.getColor(R.styleable.TopBar_centreTextColor, 0);
centreTextBackground = typedArray.getDrawable(R.styleable.TopBar_centreTextBackground);
rightText = typedArray.getString(R.styleable.TopBar_rightText);
rightTextSize = typedArray.getDimension(R.styleable.TopBar_rightTextSize, 0);
rightTextColor = typedArray.getColor(R.styleable.TopBar_rightTextColor, 0);
rightTextBackground = typedArray.getDrawable(R.styleable.TopBar_rightTextBackground);
typedArray.recycle();
initView(context);
setTextViewLocation();
setTextViewContent();
setListener();
}
public TopBarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setText(String leftName) {
leftTextView.setText(leftName);
invalidate();
}
/**
* 初始化内部控件
*
* @param context
*/
private void initView(Context context) {
leftTextView = new TextView(context);
centreTextView = new TextView(context);
rightTextView = new TextView(context);
}
/**
* 设置内部控件位置
*/
private void setTextViewLocation() {
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
leftTextView.setGravity(Gravity.CENTER_VERTICAL);
addView(leftTextView, leftParams);
centreParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
centreParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
centreTextView.setGravity(Gravity.CENTER);
addView(centreTextView, centreParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
rightTextView.setGravity(Gravity.CENTER);
addView(rightTextView, rightParams);
}
/**
* 添加数据
*/
private void setTextViewContent() {
if (leftText != null && !leftText.equals(""))
leftTextView.setText(leftText);
else
leftTextView.setText("");
leftTextView.setTextColor(leftTextColor);
leftTextView.setTextSize(leftTextSize);
leftTextView.setBackground(leftTextBackground);
if (centreText != null && !centreText.equals(""))
centreTextView.setText(centreText);
else
centreTextView.setText("");
centreTextView.setTextColor(centreTextColor);
centreTextView.setTextSize(centreTextSize);
centreTextView.setBackground(centreTextBackground);
if (rightText != null && !rightText.equals(""))
rightTextView.setText(rightText);
else
rightTextView.setText("");
rightTextView.setTextColor(rightTextColor);
rightTextView.setTextSize(rightTextSize);
rightTextView.setBackground(rightTextBackground);
setBackgroundColor(0xFFF59563);
}
public interface TopBarListener {
void leftClick(View view);
void centreClick(View view);
void rightClick(View view);
}
public void setOnClickListener(TopBarListener listener) {
this.listener = listener;
}
/**
* 添加控件点击事件
*/
private void setListener() {
leftTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick(v);
}
});
centreTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.centreClick(v);
}
});
rightTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick(v);
}
});
}
public void setLeftVisible(boolean flag) {
if (flag) {
leftTextView.setVisibility(View.VISIBLE);
} else {
leftTextView.setVisibility(View.GONE);
}
}
}</span>
3.创建activity
<span style="font-size:14px;">package zdd.huangfusheng;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Toast;
import zdd.huangfusheng.topbar.TopBarView;
public class MainActivity extends AppCompatActivity {
private TopBarView topBarView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topBarView = (TopBarView) findViewById(R.id.topBarView);
topBarView.setOnClickListener(new TopBarView.TopBarListener() {
@Override
public void leftClick(View view) {
Toast.makeText(MainActivity.this, "我是左边", Toast.LENGTH_LONG).show();
}
@Override
public void centreClick(View view) {
topBarView.setText("woshizuobian");
// topBarView.setLeftVisible(true);
}
@Override
public void rightClick(View view) {
topBarView.setText("woshizuobian");
Toast.makeText(MainActivity.this, "我是右边边", Toast.LENGTH_LONG).show();
// topBarView.setLeftVisible(false);
}
});
// TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//
// String deviceId = telephonyManager.getDeviceId();
// String androidId = Settings.System.getString(MainActivity.this.getContentResolver(), Settings.System.ANDROID_ID);
// Toast.makeText(MainActivity.this, "deviceId = " + deviceId + " androidID = " + androidId, Toast.LENGTH_LONG).show();
}
}</span>
4.创建activity的xml并且使用自定义的view。并且赋值
<span style="font-size:14px;"><?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"
tools:context="zdd.huangfusheng.MainActivity">
<zdd.huangfusheng.topbar.TopBarView
android:id="@+id/topBarView"
android:layout_width="wrap_content"
android:layout_height="100dp"
app:centreText="我是中间"
app:centreTextColor="@color/colorAccent"
app:centreTextSize="6sp"
app:leftTextBackground="@mipmap/ic_launcher"
app:leftTextColor="@color/colorPrimary"
app:leftTextSize="7sp"
app:rightText="我是右边"
/>
</RelativeLayout></span>