自定义Topbar

完成模板——



①在res / values / 下新建atts.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>


②新建class继承自Relativelayout

package com.twac.mytopbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
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 btn_left, btn_right;
	private TextView textview;

	private String title;
	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 leftParams, rightParams, titleParams;

	// ——————————————————————接口回调——————————————————————
	private topbarClickListener listener;

	public interface topbarClickListener {
		public void leftClick();

		public void rightClick();
	}

	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);
		title = ta.getString(R.styleable.Topbar_title);
		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();

		btn_left = new Button(context);
		btn_right = new Button(context);
		textview = new TextView(context);
		// 设置左侧按钮样式
		btn_left.setText(leftText);
		btn_left.setTextColor(leftTextColor);
		btn_left.setBackground(leftBackground);
		// 设置右侧按钮样式
		btn_right.setText(rightText);
		btn_right.setTextColor(rightTextColor);
		btn_right.setBackground(rightBackground);
		// 设置中间标题样式
		textview.setText(title);
		textview.setTextSize(titleTextSize);
		textview.setTextColor(titleTextColor);
		textview.setGravity(CENTER_HORIZONTAL);
		// 为ViewGroup设置背景颜色
		setBackgroundColor(0XFFF59563);

		// ——————————————————————左边button的布局属性——————————————————————
		leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
		// 把btn_left以leftParams的形式加入到ViewGroup中
		addView(btn_left, leftParams);

		// ——————————————————————textview的布局属性——————————————————————
		titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				LayoutParams.MATCH_PARENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
		// 把btn_right以rightParams的形式加入到ViewGroup中
		addView(textview, titleParams);

		// ——————————————————————右边button的布局属性——————————————————————
		rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
		// 把btn_right以rightParams的形式加入到ViewGroup中
		addView(btn_right, rightParams);

		// 定义按钮的点击事件
		btn_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.leftClick();
			}
		});

		btn_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.rightClick();
			}
		});
	}
}

③编辑Mainactivity文件

package com.twac.mytopbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.twac.mytopbar.TopBar.topbarClickListener;

public class MainActivity extends Activity {
	private TopBar mTopBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTopBar = (TopBar) findViewById(R.id.topbar);
		mTopBar.setOnTopbarClickListener(new topbarClickListener() {

			@Override
			public void rightClick() {
				Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void leftClick() {
				Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT)
						.show();
			}
		});
	}
}



④更改布局文件

——eclipse添加【xmlns:custom="http://schemas.android.com/apk/res/com.twac.mytopbar"】

 ——android studio添加【xmlns:custom="http://schemas.android.com/apk/res-auto"】


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.twac.mytopbar"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.twac.mytopbar.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        custom:leftBackground="@drawable/blue_button"
        custom:leftText="back"
        custom:leftTextColor="#FFFFFF"
        custom:rightBackground="@drawable/blue_button"
        custom:rightText="more"
        custom:rightTextColor="#FFFFFF"
        custom:title="title"
        custom:titleTextColor="#123412"
        custom:titleTextSize="10sp" >
    </com.twac.mytopbar.TopBar>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值