1.自定义属性,创建attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Topbar"> <attr name="barBackground" format="reference|color" /> <attr name="titleText" format="string" /> <attr name="titleTextSize" format="dimension" /> <attr name="titleTextColor" format="color" /> <attr name="leftText" format="string" /> <attr name="leftTextColor" format="color" /> <attr name="leftBackground" format="reference|color" /> <attr name="rightText" format="string" /> <attr name="rightTextColor" format="color" /> <attr name="rightBackground" format="reference|color" /> </declare-styleable>
package com.example.mytopbar; 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.ListAdapter; import android.widget.RelativeLayout; import android.widget.TextView; /** * Created by Zac on 2016/10/7. */ public class Topbar extends RelativeLayout { private TextView titleTv; private Button leftBtn; private Button rightBtn; private Drawable barBackground; private String titleText; private float titleTextSize; private int titleTextColor; private String leftText; private int leftTextColor; private Drawable leftBackground; private String rightText; private int rightTextColor; private Drawable rightBackground; private LayoutParams titleParams, leftParams, rightParams; private topBarClickListener listener; public interface topBarClickListener { public void leftBtnClick(); public void rightBtnClick(); } public void setOnTopbarClickListener(topBarClickListener listener) { this.listener = listener; } public Topbar(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); barBackground = ta.getDrawable(R.styleable.Topbar_barBackground); titleText = ta.getString(R.styleable.Topbar_titleText); titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0); titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0); leftText = ta.getString(R.styleable.Topbar_leftText); leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground); rightText = ta.getString(R.styleable.Topbar_rightText); rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0); rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground); ta.recycle(); titleTv = new TextView(context); leftBtn = new Button(context); rightBtn = new Button(context); titleTv.setText(titleText); titleTv.setTextSize(titleTextSize); titleTv.setTextColor(titleTextColor); titleTv.setGravity(Gravity.CENTER); leftBtn.setText(leftText); leftBtn.setTextColor(leftTextColor); leftBtn.setBackground(leftBackground); rightBtn.setText(rightText); rightBtn.setTextColor(rightTextColor); rightBtn.setBackground(rightBackground); setBackground(barBackground); titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); addView(titleTv, titleParams); addView(leftBtn, leftParams); addView(rightBtn, rightParams); leftBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listener.leftBtnClick(); } }); rightBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listener.rightBtnClick(); } }); } public void setLeftBtnVisible(boolean flag) { if (flag) { leftBtn.setVisibility(View.VISIBLE); } else { leftBtn.setVisibility(View.GONE); } } public void setRightBtnVisible(boolean flag) { if (flag) { rightBtn.setVisibility(View.VISIBLE); } else { rightBtn.setVisibility(View.GONE); } } }
3.布局文件中设置属性
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context="com.example.mytopbar.MainActivity"> <com.example.mytopbar.Topbar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="40dp" custom:barBackground="#333333" custom:titleText="自定义标题" custom:titleTextSize="10sp" custom:titleTextColor="#123412" custom:leftText="Back" custom:leftTextColor="#FFFFFF" custom:leftBackground="#333333" custom:rightText="More" custom:rightTextColor="#FFFFFF" custom:rightBackground="#333333"> </com.example.mytopbar.Topbar> </RelativeLayout>
4.引用topbar
package com.example.mytopbar; 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.setOnTopbarClickListener(new Topbar.topBarClickListener() { @Override public void leftBtnClick() { Toast.makeText(MainActivity.this, "HelloLeft", Toast.LENGTH_SHORT).show(); } @Override public void rightBtnClick() { Toast.makeText(MainActivity.this, "HelloRight", Toast.LENGTH_SHORT).show(); } }); } }