不顯示文字
//....
public class TopBar extends RelativeLayout {
private Button leftButton, rightButton;
private TextView tvTitle;
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
private float titleTextSize;
private int titleTextColor;
private String title;
private LayoutParams leftpParams,rightpParams,titlepParams;
public TopBar(Context context, AttributeSet attrs) {
super(context, 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.獲取自定義屬性
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
// 2.創建控件
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);
leftButton.setText(leftText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
rightButton.setText(rightText);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setText(title);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xfff59563);
leftpParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftpParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton, leftpParams);
rightpParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rightpParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton, rightpParams);
titlepParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
titlepParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvTitle, titlepParams);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? xmlns:app="http://schemas.android.com/apk/res/com.example.topbar.TopBar"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity" >
? ? <com.example.topbar.TopBar
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="40dp"
? ? ? ? app:leftText="left"
? ? ? ? app:rightText="right"
? ? ? ? app:title="自定義標題" >
? ? </com.example.topbar.TopBar>
</RelativeLayout>
atts代碼:
<?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="leftText" format="string" />
? ? ? ? <attr name="leftBackGround" format="reference|color" />
? ? ? ? <attr name="leftTextColor" format="color" />
? ? ? ? <attr name="rightText" format="string" />
? ? ? ? <attr name="rightBackGround" format="reference|color" />
? ? ? ? <attr name="rightTextColor" format="color" />
? ? </declare-styleable>
</resources>
2016-07-08
哥們,問題決解了嗎,我倒騰了好久,還是不行。
2016-06-09
改成下面試試 :
2016-03-30
感覺你沒有設置布局方向 ?系統默認的是設置的是橫向布局
2016-03-30
上面只是補充的代碼,剛開始沒發現可以修改問題。抱歉。
2016-03-29
atts代碼:
<?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="leftText" format="string" />
? ? ? ? <attr name="leftBackGround" format="reference|color" />
? ? ? ? <attr name="leftTextColor" format="color" />
? ? ? ? <attr name="rightText" format="string" />
? ? ? ? <attr name="rightBackGround" format="reference|color" />
? ? ? ? <attr name="rightTextColor" format="color" />
? ? </declare-styleable>
</resources>