使用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>
錯誤如圖:
萬分感謝
2022-03-24
感覺沒什么,你自己這么寫就行了唄!可以這么理解哦,您太聰明了,親
2018-07-13
你試試rebuild
2017-05-23
xmlns:app="? 加上這句
2017-03-09
我也遇到了。。。