自定义view三大步:
- 设计需要的属性
- 分析需要的自定义属性
- 在res/values/attrs.xml定义声明
- 创建需要的view
- 在layout.xml文件中进行使用
- 在View的构造方法中进行获取
- 引用创建的view
代码示例
1. 设计需要的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="topBarTitle" format="string"/>
<attr name="topBarTitleTextSize" format="dimension"/>
<attr name="topBarTitleTextColor" format="color"/>
<attr name="leftText" format="string"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftTextColor" format="color"/>
<attr name="rightText" format="string"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightTextColor" format="color"/>
</declare-styleable>
</resources>
2.创建需要的view
package com.huawaii.demo;
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.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by huawaii on 2016/1/10.
*/
public class TopBar extends RelativeLayout {
private String topBarTitle;
private float topBarTitleTextSize;
private int topBarTitleTextColor;
private String leftText;
private Drawable leftBackground;
private int leftTextColor;
private String rightText;
private Drawable rightBackground;
private int rightTextColor;
private Button leftButton, rightButton;
private TextView tvTitle;
private LayoutParams tvTitleParams, leftParams, rightParams;
private topBarButtonOnClickListener mTopBarButtonOnClickListener;
public interface topBarButtonOnClickListener {
void leftOnClick();
void rightOnClick();
}
public void setTopBarButtonOnClickListener(topBarButtonOnClickListener listener){
mTopBarButtonOnClickListener = listener;
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
topBarTitle = ta.getString(R.styleable.TopBar_topBarTitle);
topBarTitleTextSize = ta.getDimension(R.styleable.TopBar_topBarTitleTextSize, 0);
topBarTitleTextColor = ta.getColor(R.styleable.TopBar_topBarTitleTextColor, 0);
leftText = ta.getString(R.styleable.TopBar_leftText);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
leftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor, 0);
rightText = ta.getString(R.styleable.TopBar_rightText);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
rightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor, 0);
ta.recycle();
tvTitle = new TextView(context);
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle.setText(topBarTitle);
tvTitle.setTextSize(topBarTitleTextSize);
tvTitle.setTextColor(topBarTitleTextColor);
tvTitle.setGravity(Gravity.CENTER);
leftButton.setText(leftText);
leftButton.setBackground(leftBackground);
leftButton.setTextColor(leftTextColor);
rightButton.setText(rightText);
rightButton.setBackground(rightBackground);
rightButton.setTextColor(rightTextColor);
setBackgroundColor(0xfff59563);
tvTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
tvTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle, tvTitleParams);
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton, leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton, rightParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTopBarButtonOnClickListener.leftOnClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTopBarButtonOnClickListener.rightOnClick();
}
});
}
public void setLeftButtonIsVisible(boolean flag){
if (flag){
leftButton.setVisibility(VISIBLE);
}else {
leftButton.setVisibility(INVISIBLE);
}
}
}
3.引用创建的view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:huawaii="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="com.huawaii.demo.MainActivity">
<com.huawaii.demo.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
huawaii:topBarTitle="自定义标题"
huawaii:topBarTitleTextSize="10sp"
huawaii:topBarTitleTextColor="#123456"
huawaii:leftText="Back"
huawaii:leftTextColor="#ffffff"
huawaii:leftBackground="@android:color/holo_blue_light"
huawaii:rightText="More"
huawaii:rightTextColor="#ffffff"
huawaii:rightBackground="@android:color/holo_blue_light">
</com.huawaii.demo.TopBar>
</RelativeLayout>
package com.huawaii.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TopBar topBar = (TopBar)findViewById(R.id.topbar);
topBar.setTopBarButtonOnClickListener(new TopBar.topBarButtonOnClickListener() {
@Override
public void leftOnClick() {
Toast.makeText(MainActivity.this, "leftButton!", Toast.LENGTH_SHORT).show();
}
@Override
public void rightOnClick() {
Toast.makeText(MainActivity.this, "rightButton!", Toast.LENGTH_SHORT).show();
}
});
topBar.setLeftButtonIsVisible(false);
}
}
上述经典模版来源CSDN大牛-eclipse_xu,个人仅仅总结汇总,Thanks!
如果意犹未尽,可以继续学习下面文章~