-
定義atts
查看全部 -
s查看全部
-
<?xml?version="1.0"?encoding="utf-8"?> <resources> ????<declare-styleable?name="Toolbar"> ????????<attr?name="titleText"?format="string"/> ????????<attr?name="titleTextSize"?format="dimension"/> ????????<attr?name="titleTextColor"?format="color"???????? ????</declare-styleable> </resources>
private?TextView?titleView;
private?float?titleTextSize; private?int?titleTextColor; private?String?title;
private?LayoutParams?titleParam;
TypedArray?ta?=?context.obtainStyledAttributes(attrs,?R.styleable.Toolbar); title?=?ta.getString(R.styleable.Toolbar_titleText); titleTextColor?=?ta.getInt(R.styleable.Toolbar_titleTextColor,?0); titleTextSize?=?ta.getDimension(R.styleable.Toolbar_titleTextSize,?15);
ta.recycle();
titleView?=?new?TextView(context);
titleView.setText(title); titleView.setTextColor(titleTextColor); titleView.setTextSize(titleTextSize); titleView.setGravity(Gravity.CENTER);
titleParam?=?new?LayoutParams(LayoutParams.WRAP_CONTENT,?LayoutParams.MATCH_PARENT); titleParam.addRule(RelativeLayout.CENTER_IN_PARENT); addView(titleView,?titleParam);
查看全部 -
引用自定義view屬性
xmlns:定義一個命名空間=“
http://schemas.android.com/apk/res-auto"用定義的命名空間名進行引用屬性
例如xmlns:app="app:title="IMOOC" app:titleTextColor="@android:color/black" app:titleTextSize="12dp" app:leftBackgroud="@android:color/black" app:leftText="返回" app:leftTextColor="@android:color/white" app:rightBackgroud="@android:color/black" app:rightText="下一頁" app:rightTextColor="@android:color/white"
查看全部 -
package?study.com.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; /** ?*?Created?by?Administrator?on?2019/9/25. ?*/ public?class?Topbar?extends?RelativeLayout?{ ????private?Button?leftButton,?rightButton; ????private?TextView?tvTitle; ????private?int?leftTextColor; ????private?Drawable?leftBackGroud; ????private?String?leftText; ????private?int?rightTextColor; ????private?Drawable?rightBackGroud; ????private?String?rightText; ????private?float?titleTextSize; ????private?int?titleTextColor; ????private?String?title; ????private?LayoutParams?leftParams,?rightParams,?titleParams; ????public?Topbar(Context?context,?AttributeSet?attrs)?{ ????????super(context,?attrs); ????????//進行屬性關聯,?TypedArray類?取attrs下的屬集合性 ????????TypedArray?typedArray?=?context.obtainStyledAttributes(attrs,?R.styleable.Topbar); ????????//屬性賦值?根據類型選擇合適類型getColor?getDrawable?getDimension,?取值對應的key是自定義屬性名+字屬性名?,后面跟默認值 ????????leftTextColor?=?typedArray.getColor(R.styleable.Topbar_leftTextColor,?0); ????????leftBackGroud?=?typedArray.getDrawable(R.styleable.Topbar_leftBackgroud); ????????leftText?=?typedArray.getString(R.styleable.Topbar_leftText); ????????rightTextColor?=?typedArray.getColor(R.styleable.Topbar_rightTextColor,?0); ????????rightBackGroud?=?typedArray.getDrawable(R.styleable.Topbar_rightBackgroud); ????????rightText?=?typedArray.getString(R.styleable.Topbar_rightText); ????????titleTextSize?=?typedArray.getDimension(R.styleable.Topbar_titleTextSize,?0); ????????titleTextColor?=?typedArray.getColor(R.styleable.Topbar_titleTextColor,?0); ????????title?=?typedArray.getString(R.styleable.Topbar_title); ????????//typedArray使用完記得回收?避免資源浪費 ????????typedArray.recycle(); ????????leftButton?=?new?Button(context); ????????rightButton?=?new?Button(context); ????????tvTitle?=?new?TextView(context); ????????leftButton.setTextColor(leftTextColor); ????????leftButton.setText(leftText); ????????leftButton.setBackground(leftBackGroud); ????????rightButton.setTextColor(rightTextColor); ????????rightButton.setText(rightText); ????????rightButton.setBackground(rightBackGroud); ????????tvTitle.setTextColor(titleTextColor); ????????tvTitle.setTextSize(titleTextSize); ????????tvTitle.setText(title); ????????tvTitle.setGravity(Gravity.CENTER); ????????//設置父view背景色 ????????setBackgroundColor(0xFFF59563); ????????leftParams?=?new?RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ????????????????ViewGroup.LayoutParams.WRAP_CONTENT); ????????leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,?TRUE); ????????//添加子view?并設置位置屬性 ????????addView(leftButton,?leftParams); ????????rightParams?=?new?RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ????????????????ViewGroup.LayoutParams.WRAP_CONTENT); ????????rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,?TRUE); ????????addView(rightButton,?rightParams); ????????titleParams?=?new?RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ????????????????ViewGroup.LayoutParams.MATCH_PARENT); ????????titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,?TRUE); ????????addView(tvTitle,titleParams); ????} }
查看全部 -
自定義屬性三步
①設計需要的屬性
創建模塊module 在values下新建atts.xml文件,定義屬性?
<declare-styleable?name="Topbar"> ????<attr?name="title"?format="string"></attr> ????<attr?name="titleTextSize"?format="dimension"></attr> ????<attr?name="titleTextColor"?format="color"></attr> ????<attr?name="leftTextColor"?format="color"></attr> ????<attr?name="leftText"?format="string"></attr> ????<attr?name="leftBackgroud"?format="reference|color"></attr> ????<attr?name="rightTextColor"?format="color"></attr> ????<attr?name="rightText"?format="string"></attr> ????<attr?name="rightBackgroud"?format="reference|color"></attr> </declare-styleable>
②實現一個繼承的view
③引用自定義的view
查看全部 -
引用自己定義的View
動態控制自定義View的控件,回到java中給button控件添加點擊方法
接口回調
定義一個接口
定義一個方法,暴露給實現者的方法
再次修改點擊事件
1.Android Studio中只需要寫上res-auto即可 2.Eclipse中則需要加上完整的包名和控件名 3.xmlns:custom="http://schmas.android.com/apk/res-auto"
查看全部 -
20181225 注冊慕課,173xxx/32168888查看全部
-
引用自己定義的View
動態控制自定義View的控件,回到java中給button控件添加點擊方法
接口回調
定義一個接口
定義一個方法,暴露給實現者的方法
再次修改點擊事件
調用者的使用
查看全部 -
自定義View第二部,新建java文件,繼承Relativelayout,如果需要自定義屬性,就需要實現帶有AttributeSet參數的方法,否則只需要實現Context參數的方法。
聲明需要的控件,聲明控件需要的屬性,即attrs.xml中的屬性
????????3.把屬性賦值給相應的控件
4.把這個控件放到一個layout中,需要聲明每個控件的LayoutParams,并且new出來。實現控件的添加。
555
查看全部 -
topbar可以抽象一個類一樣,單獨抽取出來進行設計
自定義View,第一步首先在res/values下新建atts.xml,然后再定義View中控件相關的屬性
查看全部 -
自定義Topbar查看全部
-
atts.xml 碼 <?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>查看全部
-
設計自定義view所需要的步驟查看全部
舉報