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

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

Android UI模板設計

難度初級
時長 1小時 0分
學習人數
綜合評分9.80
167人評價 查看評價
9.9 內容實用
9.7 簡潔易懂
9.8 邏輯清晰
  • 定義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

    查看全部
    1. 引用自己定義的View

    2. 動態控制自定義View的控件,回到java中給button控件添加點擊方法

    3. 接口回調

      定義一個接口

    4. 定義一個方法,暴露給實現者的方法

    5. 再次修改點擊事件

    1.Android Studio中只需要寫上res-auto即可 2.Eclipse中則需要加上完整的包名和控件名 3.xmlns:custom="http://schmas.android.com/apk/res-auto"

    查看全部
  • 20181225 注冊慕課,173xxx/32168888
    查看全部
    1. 引用自己定義的View

      https://img1.sycdn.imooc.com//5b0d67eb00015fa010420582.jpg

      https://img1.sycdn.imooc.com//5b0d680300011a6009200566.jpg

    2. 動態控制自定義View的控件,回到java中給button控件添加點擊方法

      https://img1.sycdn.imooc.com//5b0d687b0001414210900513.jpg

    3. 接口回調

      定義一個接口

      https://img1.sycdn.imooc.com//5b0d69350001187a11050564.jpg

    4. 定義一個方法,暴露給實現者的方法

      https://img1.sycdn.imooc.com//5b0d69da00011ab110750566.jpg

    5. 再次修改點擊事件

      https://img1.sycdn.imooc.com//5b0d69fc0001b2ce10090524.jpg

    6. 調用者的使用

    https://img1.sycdn.imooc.com//5b0d6a46000175aa10750784.jpg

    查看全部
    1. 自定義View第二部,新建java文件,繼承Relativelayout,如果需要自定義屬性,就需要實現帶有AttributeSet參數的方法,否則只需要實現Context參數的方法。

    2. 聲明需要的控件,聲明控件需要的屬性,即attrs.xml中的屬性

      https://img1.sycdn.imooc.com//5b0d635f00013a4d16090780.jpg

    ????????3.把屬性賦值給相應的控件

    https://img1.sycdn.imooc.com//5b0d64110001f11414980728.jpghttps://img1.sycdn.imooc.com//5b0d6498000160b110850728.jpg

    4.把這個控件放到一個layout中,需要聲明每個控件的LayoutParams,并且new出來。實現控件的添加。

    555https://img1.sycdn.imooc.com//5b0d660e0001702d14830678.jpg

    查看全部
    1. topbar可以抽象一個類一樣,單獨抽取出來進行設計

    2. 自定義View,第一步首先在res/values下新建atts.xml,然后再定義View中控件相關的屬性

      https://img1.sycdn.imooc.com//5b0d6032000109c316260858.jpg

    查看全部
  • 自定義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所需要的步驟
    查看全部
首頁上一頁1234567下一頁尾頁

舉報

0/150
提交
取消
老師告訴你能學到什么?
通過本次課程,你將學到: 1、了解為什么要使用模板開發 2、使用模板開發的好處 3、學會自定義屬性 4、學會自定義View

微信掃碼,參與3人拼團

微信客服

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

幫助反饋 APP下載

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

公眾號

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

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!