自定义view:topBar

1.values文件夹下新建attrs.xml文件:自定义如下相关属性元素

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="topBar">
        <attr name="titleText" format="string"/>
        <attr name="titleSize" format="dimension"/>
        <attr name="titleColor" format="color"/>

        <attr name="leftText" format="string"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftTextBackground" format="reference|color"/>

        <attr name="rightText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightTextBackground" format="reference|color"/>
    </declare-styleable>
</resources>
2.自定义TopBar

package com.example.topbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TopBar extends RelativeLayout {

    private Button leftBtn,rightBtn;

    private String titleText;
    private int titleColor;
    private float titleSize;

    private String leftText;
    private Drawable leftBackGround;
    private int leftTextColor;

    private String rightText;
    private Drawable rightBackGround;
    private int rightTextColor;

    private LayoutParams leftParams,rightParams,titleParams;

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.topBar);
        titleText = array.getString(R.styleable.topBar_titleText);
        titleColor = array.getColor(R.styleable.topBar_titleColor,0);
        titleSize = array.getDimension(R.styleable.topBar_titleSize,0);

        leftText = array.getString(R.styleable.topBar_leftText);
        leftBackGround = array.getDrawable(R.styleable.topBar_leftTextBackground);
        leftTextColor = array.getColor(R.styleable.topBar_leftTextColor,0);

        rightText = array.getString(R.styleable.topBar_rightText);
        rightBackGround = array.getDrawable(R.styleable.topBar_rightTextBackground);
        rightTextColor = array.getColor(R.styleable.topBar_rightTextColor, 0);
        array.recycle();

        leftBtn = new Button(context);
        leftBtn.setText(leftText);
        leftBtn.setTextColor(rightTextColor);
        leftBtn.setBackgroundDrawable(leftBackGround);

        leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        addView(leftBtn,leftParams);

        rightBtn = new Button(context);
        rightBtn.setText(rightText);
        rightBtn.setTextColor(rightTextColor);
        rightBtn.setBackgroundDrawable(rightBackGround);

        rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addView(rightBtn,rightParams);

        TextView titleView = new TextView(context);
        titleView.setText(titleText);
        titleView.setTextColor(titleColor);
        titleView.setTextSize(titleSize);
        titleView.setGravity(Gravity.CENTER);

        titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(titleView,titleParams);
    }
}

3.应用自定义的view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.example.topbar.TopBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        my:titleText="你好"
        my:titleSize="15sp"
        my:titleColor="#ffffd0bf"
        my:leftTextColor="#ff4290ff"
        my:leftTextBackground="#ff5fff1b"
        my:leftText="左按钮"
        my:rightTextColor="#ff1f1bff"
        my:rightTextBackground="#ffff1a57"
        my:rightText="右按钮"/>

</LinearLayout>
4.效果图




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值