自定义View(1)---Topbar

来源:互联网 发布:阿里云香港服务器官网 编辑:程序博客网 时间:2024/06/05 11:44

1.新建一个xml文件attr.xml。定义出Topbar的各种属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="Topbar">        <attr name="background" format="reference|color" />        <attr name="title" format="string" />        <attr name="titleTextSize" format="dimension" />        <attr name="titleTextColor" format="color" />        <attr name="leftText" format="string" />        <attr name="leftTextColor" format="reference|color" />        <attr name="leftTextSize" format="dimension" />        <attr name="leftWidth" format="dimension" />        <attr name="leftHeight" format="dimension" />        <attr name="leftMargin" format="dimension" />        <attr name="leftBackground" format="reference|color" />        <attr name="rightText" format="string" />        <attr name="rightTextColor" format="reference|color" />        <attr name="rightTextSize" format="dimension" />        <attr name="rightWidth" format="dimension" />        <attr name="rightHeight" format="dimension" />        <attr name="rightMargin" format="dimension" />        <attr name="rightBackground" format="reference|color" />        <attr name="buttonMode">            <enum name="both" value="0" />            <enum name="left" value="1" />            <enum name="right" value="2" />        </attr>    </declare-styleable></resources>

2.dimesns.xml文件

<resources>    <!-- Default screen margins, per the Android Design guidelines. -->    <dimen name="activity_horizontal_margin">16dp</dimen>    <dimen name="activity_vertical_margin">16dp</dimen>    <!-- topbar -->    <dimen name="topbar_button_margin">12dp</dimen>    <dimen name="topbar_button_width">40dp</dimen>    <dimen name="topbar_button_height">40dp</dimen>    <dimen name="topbar_height">56dp</dimen>    <dimen name="topbar_title_text_size">7sp</dimen></resources>
3.建立Tobar类继承RelativeLayout

package com.example.topbar;import android.content.Context;import android.content.res.ColorStateList;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.util.Log;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;/** * 自定义的Topbar * 左右两边各有一个button * 中间有一个title * @author honeyYYr * 下午9:29:20 */public class Topbar extends RelativeLayout {      /**      * 按钮模式     *  -1:没有button      *  0 :有两个button     *  1 :只有leftButton     *  2 :只有rightButton      */      private int buttonMode;        private Button leftButton, rightButton;      private TextView tvTitle;    /**   * topbar 标题属性   */    private float titleTextSize;      private int titleTextColor;      private String title;    /**     * topbar左边button属性     */    private float leftMargin;     private ColorStateList leftTextColor;      private float leftTextSize;      private Drawable leftBackground;      private String leftText;      private float leftWidth;      private float leftHeight;    /**   * topbar 右边标题属性   */    private float rightMargin;     private ColorStateList rightTextColor;      private float rightTextSize;      private Drawable rightBackground;      private String rightText;      private float rightWidth;      private float rightHeight;            private LayoutParams leftParams, rightParams, titleParams;        /**     * topbar 的背景     */    private Drawable background;        private TopbarLeftClickListener leftListener;      private TopbarRightClickListener rightListener;        public interface TopbarLeftClickListener {          public void leftClick();      }        public interface TopbarRightClickListener {          public void rightClick();      }        /**     * Topbar左边button 的监听事件     * @param listener     */    public void setOnTopbarLeftClickListener(TopbarLeftClickListener listener) {          this.leftListener = listener;      }        /**     * Topbar右边button 的监听事件     * @param listener     */    public void setOnTopbarRightClickListener(TopbarRightClickListener listener) {          this.rightListener = listener;      }        public Topbar(Context context) {          this(context, null);      }       /**    * 使用自定义的属性时调用的构造函数    * @param context    * @param attrs    */    public Topbar(Context context, AttributeSet attrs) {          super(context, attrs);          TypedArray ta = context.obtainStyledAttributes(attrs,                  R.styleable.Topbar);            titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);          titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);          title = ta.getString(R.styleable.Topbar_title);            buttonMode = ta.getInteger(R.styleable.Topbar_buttonMode, -1);          background = ta.getDrawable(R.styleable.Topbar_background);            switch (buttonMode) {          case 0:// 0表示有两个button              initLeftButton(context, ta);              initRightButton(context, ta);              break;          case 1:// 1表示只有leftButton              initLeftButton(context, ta);              break;          case 2:// 2表示只有rightButton              initRightButton(context, ta);              break;          default:// 表示没有button              break;          }            ta.recycle();            setBackgroundDrawable(background);// 设置Topbar背景          tvTitle = new TextView(context);            tvTitle.setTextSize(titleTextSize);          tvTitle.setTextColor(titleTextColor);          tvTitle.setText(title);          tvTitle.setGravity(Gravity.CENTER);            titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,                  LayoutParams.MATCH_PARENT);          titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);          addView(tvTitle, titleParams);      }        /**     * 构造topbar左边button的各种属性     * @param context     * @param ta     */    @SuppressWarnings("deprecation")private void initLeftButton(Context context, TypedArray ta) {          leftTextColor = ta.getColorStateList(R.styleable.Topbar_leftTextColor);          leftTextSize = ta.getDimension(R.styleable.Topbar_leftTextSize, 0);          leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);          leftText = ta.getString(R.styleable.Topbar_leftText);          leftWidth = ta.getDimension(R.styleable.Topbar_leftWidth, 0);          leftHeight = ta.getDimension(R.styleable.Topbar_leftHeight, 0);          leftMargin = ta.getDimension(R.styleable.Topbar_leftMargin, 0);            leftButton = new Button(context);            if (leftTextColor != null)              leftButton.setTextColor(leftTextColor);          leftButton.setBackgroundDrawable(leftBackground);          leftButton.setText(leftText);          if (0 != leftTextSize)              leftButton.setTextSize(leftTextSize);            leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,                  LayoutParams.WRAP_CONTENT);          leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);          leftParams.addRule(RelativeLayout.CENTER_VERTICAL);          if (0 != leftWidth) {              leftParams.width = (int) leftWidth;          }          if (0 != leftHeight) {              leftParams.height = (int) leftHeight;          }          if (0 != leftMargin) {              leftParams.setMargins((int) leftMargin, 0, 0, 0);          }            addView(leftButton, leftParams);            leftButton.setOnClickListener(new OnClickListener() {                @Override              public void onClick(View v) {                  if (leftListener != null)                      leftListener.leftClick();              }          });      }        /**     * 构造Topbar右边Button的各种属性     * @param context     * @param ta     */    @SuppressWarnings("deprecation")private void initRightButton(Context context, TypedArray ta) {          rightTextColor = ta                  .getColorStateList(R.styleable.Topbar_rightTextColor);          rightTextSize = ta.getDimension(R.styleable.Topbar_rightTextSize, 0);          rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);          rightText = ta.getString(R.styleable.Topbar_rightText);          rightWidth = ta.getDimension(R.styleable.Topbar_rightWidth, 0);          rightHeight = ta.getDimension(R.styleable.Topbar_rightHeight, 0);          rightMargin = ta.getDimension(R.styleable.Topbar_rightMargin, 0);            rightButton = new Button(context);            if (rightTextColor != null)              rightButton.setTextColor(rightTextColor);          rightButton.setBackgroundDrawable(rightBackground);          rightButton.setText(rightText);          if (0 != rightTextSize)              rightButton.setTextSize(rightTextSize);            rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,                  LayoutParams.WRAP_CONTENT);          rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);          rightParams.addRule(RelativeLayout.CENTER_VERTICAL);          if (0 != rightWidth) {              rightParams.width = (int) rightWidth;          }          if (0 != rightHeight) {              rightParams.height = (int) rightHeight;          }          if (0 != rightMargin) {              rightParams.setMargins(0, 0, (int) rightMargin, 0);          }            addView(rightButton, rightParams);            rightButton.setOnClickListener(new OnClickListener() {                @Override              public void onClick(View v) {                  if (rightListener != null)                      rightListener.rightClick();              }          });      }        /**     *  设置标题       * @param (String)title      */    public void setTitle(String title) {          tvTitle.setText(title);      }    /**   * 设置标题   * @param (R.String.****)resId    */    public void setTitle(int resId) {          tvTitle.setText(resId);      }  }


4.使用Topbar,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.topbar"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.topbar.MainActivity" >    <com.example.topbar.Topbar        android:id="@+id/topbar"        android:layout_width="match_parent"        android:layout_height="@dimen/topbar_height"        app:background="#ffc107"        app:buttonMode="both"        app:leftBackground="@drawable/test"        app:leftHeight="@dimen/topbar_button_height"        app:leftMargin="@dimen/topbar_button_margin"        app:leftWidth="@dimen/topbar_button_width"        app:rightBackground="@drawable/test"        app:rightHeight="@dimen/topbar_button_height"        app:rightMargin="@dimen/topbar_button_margin"        app:rightWidth="@dimen/topbar_button_width"        app:title="我是谁"        app:titleTextColor="#000000"        app:titleTextSize="@dimen/topbar_title_text_size" >    </com.example.topbar.Topbar></RelativeLayout>

5.使用Topbar,MianActivity.java

package com.example.topbar;import android.app.ActionBar;import android.app.Activity;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ActionBar actionBar=getActionBar();actionBar.hide();       initTopbarEvent();      }        private void initTopbarEvent() {          Topbar topbar = (Topbar) findViewById(R.id.topbar);          topbar.setOnTopbarLeftClickListener(new Topbar.TopbarLeftClickListener() {              @Override              public void leftClick() {                  Toast.makeText(MainActivity.this, "you click left button!", Toast.LENGTH_SHORT)                          .show();              }          });            topbar.setOnTopbarRightClickListener(new Topbar.TopbarRightClickListener() {                @Override              public void rightClick() {                  Toast.makeText(MainActivity.this, "you click right button!", Toast.LENGTH_SHORT)                          .show();              }          });    }}


0 0
原创粉丝点击