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

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

使用com.example.nate.mytopbar.Topbar之后報錯

<?xml?version="1.0"?encoding="utf-8"?>
<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="???
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=".MainActivity">
????<com.example.nate.mytopbar.Topbar????????
android:id="@+id/topbar"????????
android:layout_width="match_parent"????????
android:layout_height="40dp"??
app:LeftBackground="@drawable/blue_color"???????
app:LeftText="Back"??????
app:LeftTextColor="#FFFFFF"????
app:rightBackground="@drawable/blue_color"
app:rightText="More"??
app:rightTextColor="#FFFFFF"??????
app:title="自定義標題"??????
app:titleColor="#123412"?????
app:titleSize="10sp"
>
????</com.example.nate.mytopbar.Topbar>
</RelativeLayout>

package com.example.nate.mytopbar;

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 leftcolor;
? ?private Drawable leftbackground;
? ?private String lefttext;

? ?private int rightcolor;
? ?private Drawable rightbackground;
? ?private String righttext;

? ?private int titlecolor;
? ?private float titlesize;
? ?private String titletext;
? ?
? ?private LayoutParams leftparams,rightparams,titleparams;
? ?
? ?public Topbar(Context context, AttributeSet attrs) {
? ? ? ?super(context, attrs);

? ? ? ?//拿到自己定義的屬性
? ? ? ?TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.Topbar );
? ? ? ?leftcolor=ta.getColor(R.styleable.Topbar_LeftTextColor,0);
? ? ? ?leftbackground=ta.getDrawable(R.styleable.Topbar_LeftBackground);
? ? ? ?lefttext=ta.getString(R.styleable.Topbar_LeftText);

? ? ? ?rightcolor=ta.getColor(R.styleable.Topbar_rightTextColor,0);
? ? ? ?rightbackground=ta.getDrawable(R.styleable.Topbar_rightBackground);
? ? ? ?righttext=ta.getString(R.styleable.Topbar_rightText);

? ? ? ?titlecolor=ta.getColor(R.styleable.Topbar_titleColor,0);
? ? ? ?titlesize=ta.getDimension(R.styleable.Topbar_titleSize,0);
? ? ? ?titletext=ta.getString(R.styleable.Topbar_title);

? ? ? ?//回收 1.防止浪費資源 2.避免由于一些值的緩存發生錯誤
? ? ? ?ta.recycle();

? ? ? ?//創建控件
? ? ? ?leftbutton=new Button(context);//context上下文
? ? ? ?rightbutton=new Button(context);
? ? ? ?tvtitle=new TextView(context);

? ? ? ?//為三個控件設置屬性
? ? ? ?rightbutton.setTextColor(rightcolor);
? ? ? ?rightbutton.setBackground(rightbackground);
? ? ? ?rightbutton.setText(righttext);

? ? ? ?leftbutton.setTextColor(leftcolor);
? ? ? ?leftbutton.setBackground(leftbackground);
? ? ? ?leftbutton.setText(lefttext);
? ? ? ?
? ? ? ?tvtitle.setText(titletext);
? ? ? ?tvtitle.setTextColor(titlecolor);
? ? ? ?tvtitle.setTextSize(titlesize);
? ? ? ?tvtitle.setGravity(Gravity.CENTER);

? ? ? ?//設置整體布局的背景顏色
? ? ? ?setBackgroundColor(0xFFF59563);

? ? ? ?//為左布局參數,第一屬性寬,第二屬性高
? ? ? ?leftparams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
? ? ? ?leftparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);//相對父類左對齊

? ? ? ?//添加控件到viewgroup中
? ? ? ?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"?>
<resources>
? ?<declare-styleable name="Topbar">
? ? ? ?<attr name="title" format="string"/>
? ? ? ?<attr name="titleSize" format="dimension"/>
? ? ? ?<attr name="titleColor" format="color"/>
? ? ? ?<attr name="LeftText" format="string"/>
? ? ? ?<attr name="LeftTextColor" format="color"/>
? ? ? ?<attr name="LeftBackground" format="reference|color"/>
? ? ? ?<attr name="rightText" format="string"/>
? ? ? ?<attr name="rightTextColor" format="color"/>
? ? ? ?<attr name="rightBackground" format="reference|color"/>
? ?</declare-styleable>
</resources>


錯誤如圖:
http://img1.sycdn.imooc.com//58a90d310001456303770544.jpg


萬分感謝


正在回答

4 回答

感覺沒什么,你自己這么寫就行了唄!,,

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

你試試rebuild

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

我也遇到了。。。

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

舉報

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

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

進入課程

使用com.example.nate.mytopbar.Topbar之后報錯

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

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

幫助反饋 APP下載

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

公眾號

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