package com.xiangyang.custom.ui
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.xiangyang.xcustomactionbar.R
public class XActionBar extends RelativeLayout {
private TextView XTitleTextView
private TextView XLeftTextView
private TextView XRightTextView
private ImageView XLeftImageView
private ImageView XRightImageView
private ImageView XBackground
public XActionBar(Context context, AttributeSet attrs) {
super(context, attrs)
XTitleTextView = new TextView(context)
XLeftTextView = new TextView(context)
XRightTextView = new TextView(context)
XLeftImageView = new ImageView(context)
XRightImageView = new ImageView(context)
XBackground = new ImageView(context)
// 1.设置宽高为自适应
XTitleTextView.setClickable(true)
XTitleTextView.setTextSize(R.dimen.titleSize)
XTitleTextView.setTextColor(getResources().getColor(
R.color.titleTextColor))
RelativeLayout.LayoutParams paramsXTitleTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
// 再添加位置属性
paramsXTitleTextView.addRule(RelativeLayout.CENTER_VERTICAL)
paramsXTitleTextView.addRule(RelativeLayout.CENTER_IN_PARENT)
// 向RelativeLayout添加孩子
this.addView(XTitleTextView, paramsXTitleTextView)
// 设置监听事件
// 2.设置宽高为自适应
XLeftTextView.setClickable(true)
RelativeLayout.LayoutParams paramsXLeftTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
// 再添加位置属性
paramsXLeftTextView.addRule(RelativeLayout.CENTER_VERTICAL)
paramsXLeftTextView.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
// 向RelativeLayout添加孩子
this.addView(XLeftTextView, paramsXLeftTextView)
// 3.设置宽高为自适应
XRightTextView.setClickable(true)
RelativeLayout.LayoutParams paramsXRightTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
// 再添加位置属性
paramsXRightTextView.addRule(RelativeLayout.CENTER_VERTICAL)
paramsXRightTextView.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// 向RelativeLayout添加孩子
this.addView(XRightTextView, paramsXRightTextView)
// 4.设置宽高为自适应
XLeftImageView.setClickable(true)
RelativeLayout.LayoutParams paramsXLeftImageView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
// 再添加位置属性
paramsXLeftImageView.addRule(RelativeLayout.CENTER_VERTICAL)
paramsXLeftImageView.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
// 向RelativeLayout添加孩子
this.addView(XLeftImageView, paramsXLeftImageView)
// 5.设置宽高为自适应
XRightImageView.setClickable(true)
RelativeLayout.LayoutParams paramsXRightImageView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
// 再添加位置属性
paramsXRightImageView.addRule(RelativeLayout.CENTER_VERTICAL)
paramsXRightImageView.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// 向RelativeLayout添加孩子
this.addView(XRightImageView, paramsXRightImageView)
// 开始读取xml里面设置的属性
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.XActionBar)
int indexCount = a.getIndexCount()
for (int i = 0
int attrName = a.getIndex(i)
switch (attrName) {
case R.styleable.XActionBar_isXBackground:
if (a.getBoolean(R.styleable.XActionBar_isXBackground, true)) {
XBackground.setVisibility(View.VISIBLE)
} else {
XBackground.setVisibility(View.GONE)
}
break
case R.styleable.XActionBar_XBackground:
XBackground.setImageDrawable(a
.getDrawable(R.styleable.XActionBar_XBackground))
break
case R.styleable.XActionBar_XTitleTextView:
XTitleTextView.setText(a
.getString(R.styleable.XActionBar_XTitleTextView))
break
case R.styleable.XActionBar_XLeftTextView:
XLeftTextView.setText(a
.getString(R.styleable.XActionBar_XLeftTextView))
break
case R.styleable.XActionBar_XRightTextView:
XRightTextView.setText(a
.getString(R.styleable.XActionBar_XRightTextView))
break
case R.styleable.XActionBar_isXLeftImageView:
if (a.getBoolean(R.styleable.XActionBar_isXLeftImageView, true)) {
XLeftImageView.setVisibility(View.VISIBLE)
} else {
XLeftImageView.setVisibility(View.GONE)
}
break
case R.styleable.XActionBar_XLeftImageView:
XLeftImageView.setImageDrawable(a
.getDrawable(R.styleable.XActionBar_XLeftImageView))
break
case R.styleable.XActionBar_isXRightImageView:
if (a.getBoolean(R.styleable.XActionBar_isXRightImageView, true)) {
XRightImageView.setVisibility(View.VISIBLE)
} else {
XRightImageView.setVisibility(View.GONE)
}
break
case R.styleable.XActionBar_XRightImageView:
XRightImageView.setImageDrawable(a
.getDrawable(R.styleable.XActionBar_XRightImageView))
break
case R.styleable.XActionBar_XLeftTextViewColor:
XLeftTextView.setTextColor(a.getColor(
R.styleable.XActionBar_XLeftTextViewColor, R.color.titleTextColor))
break
case R.styleable.XActionBar_XTitleTextViewColor:
XTitleTextView.setTextColor(a.getColor(
R.styleable.XActionBar_XTitleTextViewColor, R.color.titleTextColor))
break
case R.styleable.XActionBar_XRightTextViewColor:
XRightTextView.setTextColor(a.getColor(
R.styleable.XActionBar_XRightTextViewColor, R.color.titleTextColor))
break
case R.styleable.XActionBar_XLeftTextViewTextSize:
XLeftTextView.setTextSize(a.getDimension(
R.styleable.XActionBar_XLeftTextViewTextSize,
R.dimen.titleSize))
break
case R.styleable.XActionBar_XTitleTextViewTextSize:
XTitleTextView.setTextSize(a.getDimension(
R.styleable.XActionBar_XTitleTextViewTextSize,
R.dimen.titleSize))
break
case R.styleable.XActionBar_XRightTextViewTextSize:
XRightTextView.setTextSize(a.getDimension(
R.styleable.XActionBar_XRightTextViewTextSize,
R.dimen.titleSize))
break
default:
break
}
}
// 刷新
a.recycle()
}
//监听事件
public TextView getXTitleTextView() {
return XTitleTextView
}
public TextView getXLeftTextView() {
return XLeftTextView
}
public ImageView getXLeftImageView() {
return XLeftImageView
}
public TextView getXRightTextView() {
return XRightTextView
}
public ImageView getXRightImageView() {
return XRightImageView
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="XActionBar">
<attr name="isXBackground" format="boolean" />
<attr name="XBackground" format="reference" />
<attr name="isXLeftImageView" format="boolean" />
<attr name="isXTitleImageView" format="boolean" />
<attr name="isXRightImageView" format="boolean" />
<attr name="XLeftTextView" format="string" />
<attr name="XLeftImageView" format="reference" />
<attr name="XTitleTextView" format="string" />
<attr name="XTitleImageView" format="reference" />
<attr name="XRightTextView" format="string" />
<attr name="XRightImageView" format="reference" />
<attr name="XLeftTextViewColor" format="color" />
<attr name="XTitleTextViewColor" format="color" />
<attr name="XRightTextViewColor" format="color" />
<attr name="XLeftTextViewTextSize" format="dimension" />
<attr name="XTitleTextViewTextSize" format="dimension" />
<attr name="XRightTextViewTextSize" format="dimension" />
<attr name="XLeftImage_width" format="dimension" />
<attr name="XLeftImage_height" format="dimension" />
<attr name="XTitleImage_width" format="dimension" />
<attr name="XTitleImage_height" format="dimension" />
<attr name="XRightImage_width" format="dimension" />
<attr name="XRightImage_right" format="dimension" />
<attr name="XPadding" format="dimension" />
</declare-styleable>
</resources>
实例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical" >
<com.xiangyang.custom.ui.XActionBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:padding="5dp"
app:XLeftTextView="返回"
app:XLeftTextViewColor="@color/subTitleTextColor"
app:XLeftTextViewTextSize="@dimen/subTitleSize"
app:XRightTextView="设置"
app:XRightTextViewColor="@color/subTitleTextColor"
app:XRightTextViewTextSize="@dimen/subTitleSize"
app:XTitleTextView="标题"
app:XTitleTextViewColor="#FFF"
app:XTitleTextViewTextSize="@dimen/titleSize"
app:isXBackground="true" >
</com.xiangyang.custom.ui.XActionBar>
</LinearLayout>