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

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

完全按照視頻來的,但是運行出來左右button都不顯示,有人知道怎么解決么?

package?com.example.acer.topbar_demo;

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;
????private?Button?rightButton;
????private?TextView?tvTitle;

????private?int?leftTextColor;
????private?String?leftText;
????private?Drawable?leftBackground;

????private??int?rightTextColor;
????private??String?rightText;
????private??Drawable?rightBackground;

????private??float?titleTextSize;
????private??int?mTitleTextColor;
????private??String?titleText;

????private??LayoutParams?leftParams,?rightParams,?titleParams;
????public?Topbar(Context?context,?AttributeSet?attr){
????????super(context,attr);

????????TypedArray?ta?=?context.obtainStyledAttributes(attr,R.styleable.Topbar);
????????leftTextColor?=?ta.getColor(R.styleable.Topbar_leftTextColor,?0);
????????leftText?=?ta.getString(R.styleable.Topbar_leftText);
????????leftBackground?=?ta.getDrawable(R.styleable.Topbar_leftBackground);

????????titleTextSize?=?ta.getDimension(R.styleable.Topbar_titleTextSize,?0);
????????titleText?=?ta.getString(R.styleable.Topbar_title);
????????mTitleTextColor?=?ta.getColor(R.styleable.Topbar_mTitleTextColor,?0);

????????rightText?=?ta.getString(R.styleable.Topbar_rightText);
????????rightTextColor?=?ta.getColor(R.styleable.Topbar_rightTextColor,?0);
????????rightBackground?=?ta.getDrawable(R.styleable.Topbar_rightBackground);

????????ta.recycle();

????????leftButton?=?new?Button(context);
????????rightButton?=?new?Button(context);
????????tvTitle?=?new?TextView(context);

????????leftButton.setTextColor(leftTextColor);
????????leftButton.setBackground(leftBackground);
????????leftButton.setText(leftText);

????????rightButton.setText(rightText);
????????rightButton.setBackground(rightBackground);
????????rightButton.setTextColor(rightTextColor);

????????tvTitle.setTextColor(mTitleTextColor);
????????tvTitle.setText(titleText);
????????tvTitle.setTextSize(titleTextSize);
????????tvTitle.setGravity(Gravity.CENTER);

????????setBackgroundColor(0xFFF59563);

????????leftParams?=?new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,?ViewGroup.LayoutParams.WRAP_CONTENT);
????????leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,?TRUE);

????????addView(leftButton,?leftParams);

????????rightParams?=?new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,?ViewGroup.LayoutParams.WRAP_CONTENT);
????????rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,?TRUE);

????????addView(rightButton,?rightParams);

????????titleParams?=?new?LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,?ViewGroup.LayoutParams.MATCH_PARENT);
????????titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,?TRUE);

????????addView(tvTitle,?titleParams);
????}
}


<?xml?version="1.0"?encoding="utf-8"?>
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????xmlns:custom="http://schemas.android.com/apk/res-auto"
????xmlns:tools="http://schemas.android.com/tools"
????android:layout_width="match_parent"
????android:layout_height="match_parent"
????android:padding="5dp"
????tools:context="com.example.acer.topbar_demo.MainActivity">

????<!--<TextView-->
????????<!--android:layout_width="wrap_content"-->
????????<!--android:layout_height="wrap_content"-->
????????<!--android:text="Hello?World!"?/>-->
????<android.support.v7.widget.Toolbar
????????android:id="@+id/topbar"
????????android:layout_width="match_parent"
????????android:layout_height="40dp"

????????custom:leftBackground="@color/colorAccent"
????????custom:leftText="Back"
????????custom:leftTextColor="#ffffff"

????????custom:rightBackground="@color/colorPrimaryDark"
????????custom:rightText="More"
????????custom:rightTextColor="#ffffff"

????????custom:title="Myself"
????????custom:mTitleTextColor="#ffffff"
????????custom:titleTextSize="10dp">

????</android.support.v7.widget.Toolbar>
</RelativeLayout>


<?xml?version="1.0"?encoding="utf-8"?>
<resources>
????<attr?name="title"?format="string"/>
????<attr?name="titleTextSize"?format="dimension"/>
????<attr?name="mTitleTextColor"?format="color"/>
????<declare-styleable?name="Topbar">
????????<attr?name="title"/>
????????<attr?name="titleTextSize"/>
????????<attr?name="mTitleTextColor"/>

????????<attr?name="leftTextColor"?format="color"/>
????????<attr?name="leftText"?format="string"/>
????????<attr?name="leftBackground"?format="reference|color"/>

????????<attr?name="rightText"?format="string"/>
????????<attr?name="rightBackground"?format="reference|color"/>
????????<attr?name="rightTextColor"?format="color"/>
????</declare-styleable>
</resources>


http://img1.sycdn.imooc.com//57e5e93f0001fb8102650425.jpg

正在回答

2 回答

這塊<android.support.v7.widget.Toolbar
? ? ? ?
????</android.support.v7.widget.Toolbar>

你是不是引用錯了呀?應該引用的是你自定義的

<com.example.mytopbar.Topbar
??
</com.example.mytopbar.Topbar>

而不是系統自帶的。

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

接口沒有看到啊

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

舉報

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

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

進入課程

完全按照視頻來的,但是運行出來左右button都不顯示,有人知道怎么解決么?

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

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

幫助反饋 APP下載

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

公眾號

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