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

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

顯示不出來效果

public?class?TopBar?extends?RelativeLayout?{

	private?Button?leftButton;//左邊按鈕
	private?Button?rightButton;//右邊按鈕
	private?TextView?tvTitle;???//中間標題
	
	//聲明一些控件的屬性
	private?int?leftTextColor;
	private?Drawable?leftBackground;
	private?String?leftText;
	
	private?int?rightTextColor;
	private?Drawable?rightBackground;
	private?String?rightText;
	
	private?int?titleTextColor;
	private?String??title;
	private?float?titleTextSize;
	
	//為三個布局形式
	private?LayoutParams?leftParams,rightParams,titleParams;
	
	@SuppressLint("NewApi")?public?TopBar(Context?context,?AttributeSet?attrs)?{
		super(context,?attrs);
		Log.i("info",?"調用了自定義的屬性");
		//拿到自己定義的屬性
		TypedArray?ta=context.obtainStyledAttributes(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);
		
		titleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor,?0);
		title=ta.getString(R.styleable.TopBar_title);
		titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,?15);
				
		//回收掉??1、有可能浪費資源。2、避免由于一些直的緩存發生的錯誤
		ta.recycle();
		
		//創建控件
		leftButton=new?Button(context);
		rightButton=new?Button(context);
		tvTitle=new?TextView(context);
		
		//為三個空間設置屬性
		leftButton.setTextColor(leftTextColor);
		leftButton.setBackground(leftBackground);//注意最小的sdk為16
		leftButton.setText(leftText);
		
		rightButton.setTextColor(rightTextColor);
		rightButton.setBackground(rightBackground);//注意最小的sdk為16
		rightButton.setText(rightText);
		
		tvTitle.setText(title);
		tvTitle.setTextColor(titleTextColor);
		tvTitle.setTextSize(titleTextSize);
		tvTitle.setGravity(Gravity.CENTER);//自己添加的屬性使其居中
		
		//設置整體布局的背景顏色
		setBackgroundColor(0x6699ff);
		
		//為左部局參數
		//第一個屬性為寬,第二個為高
		leftParams=new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
		//添加控件
		addView(leftButton,leftParams);
		
		//為右邊局參數
		rightParams=new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_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);
	}
}
<?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="leftTextColor"?format="color"/>
?????????<attr?name="leftBackground"?format="reference|color"/>
?????????<attr?name="leftText"?format="string"/>
?????????<attr?name="rightTextColor"?format="color"/>
?????????<attr?name="rightBackground"?format="reference|color"/>
?????????<attr?name="rightText"?format="string"/>
?????????
?????</declare-styleable>
????
</resources>
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????xmlns:tools="http://schemas.android.com/tools"
????android:layout_width="match_parent"
????android:layout_height="match_parent"?
????xmlns:app="http://schemas.android.com/apk/res/com.fdd.topbar">

????<com.fdd.topbar.TopBar
????????android:id="@+id/topbar"
????????android:layout_width="match_parent"
????????android:layout_height="50dp"
????????app:leftBackground="#000000"
????????app:leftText="左部按鈕"
????????app:leftTextColor="#ff0000"
????????app:rightBackground="#00000f"
????????app:rightText="右部按鈕"
????????app:rightTextColor="#ff00ff"
????????app:title="標題"
????????app:titleTextColor="#00ff00"
????????app:titleTextSize="25sp"?/>

</RelativeLayout>


正在回答

3 回答

是改成auto ?老師說elipse不是用auto的 ? 可是事實是要用auto ?

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

應該改成xmlns:app="http://schemas.android.com/apk/res-auto,我復制沒注意^_^

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

馮冬冬 提問者

還是不行
2016-12-28 回復 有任何疑惑可以回復我~

在xml布局引用中,自定義命名空間app寫錯了,可以改為xmlns:android="http://schemas.android.com/apk/res-auto,同學記得多注意些重要細節哦!

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

舉報

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

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

進入課程

顯示不出來效果

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

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

幫助反饋 APP下載

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

公眾號

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