自定义TitleBar 控件

来源:互联网 发布:蓝牙与单片机通信 编辑:程序博客网 时间:2024/04/20 02:53

以前觉得自定义控件好神奇,当自己懂了以后才发现,其实也没什么技术可以言了

在values中创建一个demens.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="TopBar">        <attr name="title" format="string"/>        <attr name="titleTextSize" format="dimension"/>        <attr name="titleTextColor" format="color"/>        <attr name="leftTextColor" format="color"/>        <attr name="leftBackground" format="reference|color"/>        <attr name="leftText" format="string"/>        <attr name="rightTextColor" format="color"/>        <attr name="rightBackground" format="reference|color"/>        <attr name="rightText" format="string"/>    </declare-styleable></resources>

创建一个类 Topbar.java
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;import android.widget.Toast;import com.addcn.TestApp.R;/** * 自定义Titlebar */public class Topbar extends RelativeLayout {    private Button leftButton, rightButton;    private TextView tvTitle;    private int leftTextColor;    private Drawable leftBackground;    private String leftText;    private int rightTextColor;    private Drawable rightBackground;    private String rightText;    private float titleTextSize;    private int titleTextColor;    private String title;    private LayoutParams leftParams;    private LayoutParams rightParams;    private LayoutParams titleParams;    private TopbarClickListener listener;    /**     * 定义回调事件     */    public interface TopbarClickListener {        public void leftClick(View view);        public void rightClick(View view);    }    public void setOnTopBarClickListener(TopbarClickListener topBarClickListener){        listener=topBarClickListener;    }    public Topbar(final Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);        leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);        leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);        leftText = ta.getString(R.styleable.TopBar_leftText);        rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);        rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);        rightText = ta.getString(R.styleable.TopBar_rightText);        titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 0);        titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);        title = ta.getString(R.styleable.TopBar_title);        ta.recycle();        leftButton = new Button(context);        rightButton = new Button(context);        tvTitle = new TextView(context);        leftButton.setTextColor(leftTextColor);        leftButton.setBackground(leftBackground);        leftButton.setText(leftText);        rightButton.setTextColor(rightTextColor);        rightButton.setBackground(rightBackground);        rightButton.setText(rightText);        tvTitle.setTextSize(titleTextSize);        tvTitle.setTextColor(titleTextColor);        tvTitle.setText(title);        tvTitle.setGravity(Gravity.CENTER);        setBackgroundColor(0xFFF59563);        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);        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);        addView(tvTitle, titleParams);        leftButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                listener.leftClick(view);            }        });        rightButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                listener.rightClick(view);            }        });    }}

在自己使用到的布局中添加控件 如:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              <span style="color:#ff0000;">xmlns:custom="http://schemas.android.com/apk/res-auto"</span>              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">    <com.addcn.tool.Topbar            android:id="@+id/topbar"            android:layout_width="match_parent"            android:layout_height="40dp"            custom:leftBackground="@drawable/chatfrom_bg_normal"            custom:leftText="Back"            custom:leftTextColor="#FFFFFF"            custom:rightBackground="@drawable/chatto_bg_normal"            custom:rightText="More"            custom:rightTextColor="#FFFFFF"            custom:title="自定义标题"            custom:titleTextColor="#123412"            custom:titleTextSize="10sp">    </com.addcn.tool.Topbar></LinearLayout>

在这个布局中需要添加:

<span style="color:#ff0000;">xmlns:custom="http://schemas.android.com/apk/res-auto"</span>



ps:com.addcn.tool 这是我自己的包名 ,在使用的时候,替换成自己Topbar.java 目录就好。


在自己Activity中使用的时候 就跟普通的控件一样 如:

Topbar topbar = (Topbar) findViewById(R.id.topbar);        topbar.setOnTopBarClickListener(new Topbar.TopbarClickListener() {            @Override            public void leftClick(View view) {                Toast.makeText(context,"left",Toast.LENGTH_LONG).show();            }            @Override            public void rightClick(View view) {                Toast.makeText(context,"right",Toast.LENGTH_LONG).show();            }        });



技能重要,思想更加重要,在自定义控件的时候,多去想想系统是如何做的,看看源码怎么实现的(虽然我没去看)



0 0
原创粉丝点击