自创控件Topbar

<?xml version="1.0" encoding="utf-8"?>
<!-- 在Values中新建atts.xml文件 -->
<resources>
    <declare-styleable name="Topbar" >
        <attr name="title" 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>

</resources>

//再写一个类

 

public class Topbar extends RelativeLayout{

    private Button leftButton, rightButton;
    private TextView textView;

    private int leftTextColor, rightTextColor;
    private Drawable leftTextBackground, rightTextBackground;
    private String leftText, rightText;

    private float titleTextSize;
    private int titleTextColor;
    private String titles;

    private LayoutParams leftP, rightP, titleP;


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

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Topbar);

        leftTextColor = typedArray.getColor(R.styleable.Topbar_leftTextColor, 0);
        rightTextColor = typedArray.getColor(R.styleable.Topbar_rightTextColor, 0);

        leftTextBackground = typedArray.getDrawable(R.styleable.Topbar_leftBackground);
        rightTextBackground = typedArray.getDrawable(R.styleable.Topbar_rightBackground);

        leftText = typedArray.getString(R.styleable.Topbar_leftText);
        rightText = typedArray.getString(R.styleable.Topbar_rightText);


        titleTextSize = typedArray.getDimension(R.styleable.Topbar_titleTextSize, 0);
        titleTextColor = typedArray.getColor(R.styleable.Topbar_titleTextColor, 0);
        titles = typedArray.getString(R.styleable.Topbar_title);

        typedArray.recycle();

        leftButton = new Button(context);
        rightButton = new Button(context);
        textView = new TextView(context);

        leftButton.setText(leftText);
        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftTextBackground);

        rightButton.setText(rightText);
        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightTextBackground);

        textView.setText(titles);
        textView.setTextColor(titleTextColor);
        textView.setTextSize(titleTextSize);
        textView.setGravity(Gravity.CENTER);

        setBackgroundColor(0xFFF59563);

        leftP = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        leftP.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftButton, leftP);

        rightP = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        rightP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightButton, rightP);

        titleP = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        titleP.addRule(RelativeLayout.CENTER_HORIZONTAL, TRUE);
        addView(textView, titleP);
        
    }
}

//使用

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.qihao.mytopbar.MainActivity">

    <com.qihao.mytopbar.Topbar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        my:leftBackground="#ffffff"
        my:leftText="Back"
        my:leftTextColor="#00FF00"

        my:rightBackground="#0000FF"
        my:rightText="Setting"
        my:rightTextColor="#FFFFFF"

        my:title="QiHao"
        my:titleTextColor="#FF0000"
        my:titleTextSize="10sp">

    </com.qihao.mytopbar.Topbar>
</RelativeLayout>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值