左右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>
2016-06-09
在addRule這里,參數你寫錯了。
2016-05-11
和你一樣
2016-03-27
在模擬器上運行看看效果