国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

菜鳥筆記的第一天,自定義View(組合控件)

2019-11-09 15:42:04
字體:
來源:轉載
供稿:網友

~~~~新的一年已經開始,每個人都做好了自己的人生規劃,開啟了新的航程。回過頭來,我發現做程序員已經有四五年的光景了,可是捫心自問,我并沒有從菜鳥進化成大牛。所以趁我還年輕,我決定改變我現在的生活狀態,我希望能像鴻神所說的那樣,通過寫文章來讓自己變得很厲害。 大家有沒有發現,很多技術點我們用起來很6,但是卻總是遇到奇奇怪怪的bug或者問題,在面試的時候如果面試官讓你去講,很有可能講兩三分鐘就講完了,而且答的并不理想。最根本的原因就是,我們并沒有完全get這個點。如果你再去研究,肯定會有意想不到的收獲。 扯淡扯了這么久,下面開始進入正題。


Android自定義View主要包括3種實現方式:組合控件、繪制控件、繼承控件。那今天我呢就主要來聊一下組合控件吧~第一步,當然是畫布局頁面了<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/ds_30" android:paddingRight="@dimen/ds_30" android:layout_marginBottom="@dimen/ds_3" android:gravity="center_vertical" android:orientation="horizontal" android:background="#fff"> <ImageView android:id="@+id/mine_item_img" android:layout_width="@dimen/ds_81" android:layout_height="@dimen/ds_81" android:background="@mPRivate TextView countView; //構造方法 public MineItemView(Context context) { super(context); } public MineItemView(Context context, AttributeSet attrs) { super(context, attrs); initView(context, attrs); } public MineItemView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs); } //初始化View private void initView(Context context, AttributeSet attrs) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.widget_mine_item, this, true); ImageView leftImg = (ImageView) view.findViewById(R.id.mine_item_img); TextView titleView = (TextView) view.findViewById(R.id.mine_item_title); countView = (TextView) view.findViewById(R.id.mine_item_count); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MineItemView); Drawable leftDrawable = typedArray.getDrawable(R.styleable.MineItemView_leftImg); String title = typedArray.getString(R.styleable.MineItemView_title); String count = typedArray.getString(R.styleable.MineItemView_count); typedArray.recycle(); if(leftDrawable == null){ leftDrawable = ContextCompat.getDrawable(context, R.mipmap.ic_launcher); } leftImg.setBackground(leftDrawable); if(TextUtils.isEmpty(title)){ title = ""; } titleView.setText(title); if(TextUtils.isEmpty(count)){ count = ""; } countView.setText(count); }第四步,在我們的主頁面布局中引入自定義屬性,添加自定義控件并給自定義的屬性賦值:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" <com.bryan.app.MineItemView android:layout_width="match_parent" android:layout_height="@dimen/ds_144" app:leftImg="@mipmap/ic_launcher" app:count="8" app:title="我的消息" /><LinearLayout />

注意:xmlns:app=”http://schemas.android.com/apk/res-auto” 中的“app”可以隨意命名,只需要控件中的與之對應即可。代碼敲到這里,我們先喝杯咖啡,看看運行效果:

運行效果


很輕松愉快的我們就完成了一個實用的自定義View,當然我們可以用同樣的方式做一些更復雜的View,今天就到這里吧~*初來乍到,一心向學,如有問題,煩請指正。*
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永春县| 永仁县| 乌拉特后旗| 疏勒县| 安化县| 通化县| 宝鸡市| 通城县| 宁城县| 商城县| 宁明县| 南召县| 麻江县| 津市市| 南溪县| 巴中市| 柳河县| 湄潭县| 鄱阳县| 阿拉善盟| 和龙市| 沭阳县| 宜兰县| 邵阳市| 新巴尔虎左旗| 永新县| 绵竹市| 当雄县| 宽甸| 阿克| 台东市| 阳高县| 赣榆县| 南京市| 奈曼旗| 鄱阳县| 通城县| 志丹县| 平定县| 怀仁县| 遂川县|