左右Button不出现,有人知道怎么解决吗?
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.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Topbar extends RelativeLayout{
private Button leftButton,rightButton;
private TextView tvTitle;
//左边button的属性
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
//右边button的属性
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
//Title属性
private float titleTextSize;
private int titleTextColor;
private String title;
private LayoutParams leftParams,rightParams,titleParams;//布局属性
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
// 思路:在构造方法中获取我们在xml文件自定义的那些属性
//并把这些属性值赋给我们的控件
//TypedArray是一个数据结构
//context.obtainStyledAttributes(set, attrs)传入xml文件中的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();//回收数据:1.避免浪费资源;2、避免由于换成你而发生错误
//实例化
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);//给viewGroup设置背景颜色
leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);//传入长宽值
leftParams.addRule(RelativeLayout.ALIGN_LEFT,TRUE);//给leftParams添加一个规则:居左对齐
addView(leftButton, leftParams);//调用ViewGroup中的addView方法,使得leftButton以leftParams的形式加入到viewGroup中
rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_RIGHT,TRUE);
addView(rightButton, rightParams);
titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvTitle, titleParams);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-com.example.topbar.Topbar"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.topbar.MainActivity" >
<com.example.topbar.Topbar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="50dp"
custom:leftText="Back"
custom:leftBackground="@drawable/leftbutton"
custom:leftTextColor="#FFFFFF"
custom:rightText="More"
custom:rightBackground="@drawable/leftbutton"
custom:rightTextColor="#FFFFFF"
custom:title="自定义标题"
custom:titleTextSize="10sp"
custom:titleTextColor="#123412"
></com.example.topbar.Topbar>
</RelativeLayout>