attrs.xml(第一步,在这里)
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- R.styleable.TopBar_titleTextColor在R文件中是这样的形式 --> <declare-styleable name="TopBar"> <!-- 文字 --> <attr name="leftText" format="string" /> <attr name="rightText" format="string" /> <attr name="titleText" format="string" /> <!-- 文字颜色 --> <attr name="leftTextColor" format="color" /> <attr name="rightTextColor" format="color" /> <attr name="titleTextColor" format="color" /> <!-- 文字大小 --> <attr name="titleTextSize" format="dimension" /> <!-- 背景色 --> <attr name="leftBackground" format="reference|color" /> <attr name="rightBackground" format="reference|color" /> </declare-styleable> </resources>
activity_main.xml(命名空间需要注意)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res/com.example.custometopbarui" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.sloop.topbar.MainActivity" > <com.example.custometopbarui.TopBar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="48dp" app:leftBackground="@drawable/ic_launcher" app:leftText="左侧" app:rightBackground="@drawable/ic_launcher" app:rightText="右侧" app:titleText="自定义标题" app:titleTextColor="#ffffff" app:titleTextSize="8sp" > </com.example.custometopbarui.TopBar> </RelativeLayout>
TopBar(注释很详细)
package com.example.custometopbarui; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; 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; public class TopBar extends RelativeLayout { /** * 定义三个控件, */ private Button leftButton, rightButton; private TextView title; //左侧button属性 private int leftTextColor; private Drawable leftBackground; private String leftText; //右侧button属性 private int rightTextColor; private Drawable rightBackground; private String rightText; //title属性 private int titleTextColor; private float titleTextSize; private String titleText; //因为容器 private LayoutParams leftParams, rirhtParams, titleParams; //定义接口对象 private TopBarClickListener listener; /** * 点击事件监听器接口 * @author Administrator * */ public interface TopBarClickListener { public void leftclick(); public void rightclick(); } //设置监听器 public void setOnTopBarClickListener(TopBarClickListener listener){ this.listener = listener; } /** * 对外暴露的方法 * @param visible */ public void setLeftIsVisible(boolean visible){ if (visible) { leftButton.setVisibility(View.VISIBLE); } else { leftButton.setVisibility(View.GONE); } } public void setRightIsVisible(boolean visible){ if (visible) { rightButton.setVisibility(View.VISIBLE); } else { rightButton.setVisibility(View.GONE); } } /** * 构造器 * @param context * @param attrs */ public TopBar(Context context, AttributeSet attrs){ super(context, attrs); //获取自定义属性和值的映射集合 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar); //取出自定义属性 - 左侧 leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, Color.BLACK); leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground); leftText = ta.getString(R.styleable.TopBar_leftText); //取出自定义属性 - 右侧 rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, Color.BLACK); rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground); rightText = ta.getString(R.styleable.TopBar_rightText); //取出自定义属性 - 标题 titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, Color.BLACK); titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 12); titleText = ta.getString(R.styleable.TopBar_titleText); //回收TypedArray(避免浪费资源,避免因为缓存导致的错误) ta.recycle(); /** * 创建控件对象 */ leftButton = new Button(context); rightButton = new Button(context); title = new TextView(context); /** * 为各个控件设置属性 - 左侧 */ leftButton.setText(leftText); leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackground); //设置属性 - 右侧 rightButton.setText(rightText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackground); //设置属性 - 标题 title.setText(titleText); title.setTextSize(titleTextSize); title.setTextColor(titleTextColor); title.setGravity(Gravity.CENTER); //设置整体背景颜色 setBackgroundColor(0xfff59563); /** * 为各个控件设置布局 - 左 */ //设置宽高 leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //添加规则 leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); addView(leftButton, leftParams);//将按钮添加进布局中 //设置布局 - 右 rirhtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rirhtParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); rirhtParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); addView(rightButton, rirhtParams);//将按钮添加进布局中 //设置布局 - 标题 titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); addView(title, titleParams);//将按钮添加进布局中 /** * 设置监听器 */ leftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ //这里写一个接口回调方法,让外部去实现即可 listener.leftclick(); } }); rightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ listener.rightclick(); } }); } }
MainActivity
package com.example.custometopbarui; import com.example.custometopbarui.TopBar.TopBarClickListener; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TopBar topBar = (TopBar) findViewById(R.id.topbar); topBar.setLeftIsVisible(false); // topBar.setRightIsVisible(false); topBar.setOnTopBarClickListener(new TopBarClickListener() { @Override public void rightclick(){ Toast.makeText(MainActivity.this, "Right Clicked", Toast.LENGTH_SHORT).show(); } @Override public void leftclick(){ Toast.makeText(MainActivity.this, "Left Clicked", Toast.LENGTH_SHORT).show(); } }); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-25 13:28:08