亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

不顯示文字

//....

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>


正在回答

5 回答

哥們,問題決解了嗎,我倒騰了好久,還是不行。

0 回復 有任何疑惑可以回復我~
xmlns:app="http://schemas.android.com/apk/res/com.example.topbar.TopBar"

改成下面試試 :

xmlns:app="http://schemas.android.com/apk/res-auto"


0 回復 有任何疑惑可以回復我~

感覺你沒有設置布局方向 ?系統默認的是設置的是橫向布局

0 回復 有任何疑惑可以回復我~
#1

qq_陽光下的躺椅 提問者

和布局方向沒關系吧,就算是和布局方向有關,也應該顯示,只是顯示有問題而已。
2016-03-30 回復 有任何疑惑可以回復我~

上面只是補充的代碼,剛開始沒發現可以修改問題。抱歉。

0 回復 有任何疑惑可以回復我~

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>


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android UI模板設計
  • 參與學習       76032    人
  • 解答問題       233    個

快來學習如何在Android中自定義View,本次課程一定會讓你獲益匪淺

進入課程

不顯示文字

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號