在我们实际开发其中。会碰见一些布局结构类似或者同样的界面。比如应用的设置界面、tabbutton界面等。

这时候。对于刚開始学习的人来说,xml里面一个个绘制出来也许是最初的想法。可能随着经验的积累,又学会一招。就是使用include标签,导入类似或者同样的布局,提高了性能又降低了代码;再以后呢,自己定义控件又能够实现这一目的。本文就是简单的使用自己定义的组合控件模仿猫眼底部菜单条。

1.自己定义组合控件属性:在res/values文件夹下创建attrs.xml文件

  1. <declare-styleable name="TabItemView">
  2. <attr name="contentTextSize" format="dimension"/> <!-- 字体大小 -->
  3. <attr name="contentTextColor" format="color"/> <!-- 字体颜色 -->
  4. <attr name="contentTextString" format="string"/> <!-- 显示的默认文字 -->
  5. <attr name="contentLogoBack" format="reference"/> <!-- item背景 -->
  6. <attr name="contentLogoSize" format="dimension" />
  7. </declare-styleable>

TabItemView:简单一点说,就是属性组合的名字。

<attr />某个属性的定义:如<attr name="contentTextSize" format="dimension"/>,定义的就是文字的大小。contentTextSize指属性名字(和我们xml中经常使用的textSize)。format定义的是contentTextSize详细的属性类型。

以下简单的说下format的详细类型:

(1) reference:參考某一资源ID。 (2) color:颜色值。

(3) boolean:布尔值。 (4) dimension:尺寸值。

(5) float:浮点值。

(6) integer:整型值。

(7) string:字符串。 (8) fraction:百分数。

(9) enum:枚举值。 (10)flag:位或运算。

2.控件的属性定义完了,然后就是要在代码里面获取使用这些属性,看源码的时候。你会发现系统定义的属性都是通过TypedArray这玩意获取的,获取方法例如以下:

  1. TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TabItemView);

以上方法返回的我们自己定义的属性集合,待最后用完之后,须要手动释放一下。ta.recycle();

TypedArray属性集合得到之后。以下就是依据须要获取不同的属性,针对不同的属性有不同的获取方法。以下是几个比較经常使用到的属性获取方法:

(1)getDimensionPixelSize:获取尺寸的大小(间距。文字大小等)

(2)getResourceId:获取资源id(图片等)

(3)getString:获取字符串

(4)getBoolean:获取布尔值

(5)getColor:获取颜色值

(6)getFloat:获取浮点类型值

以下是TabItemView自己定义组合控件的完整代码:

  1. package com.dandy.weights;
  2.  
  3. import com.dandy.utils.PhoneUtils;
  4. import com.demo.dandy.R;
  5. import android.content.Context;
  6. import android.content.res.TypedArray;
  7. import android.text.TextUtils;
  8. import android.util.AttributeSet;
  9. import android.util.TypedValue;
  10. import android.view.InflateException;
  11. import android.view.View;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15. import android.view.View.OnClickListener;
  16.  
  17. public class TabItemView extends LinearLayout implements OnClickListener{
  18.  
  19. private Context mContext;
  20.  
  21. private ImageView contentLogo;
  22. private TextView contentText;
  23.  
  24. private int logoBackResourceId;
  25. private String textString;
  26. private int textColor;
  27. private float textSize;
  28. private int contentLogoSize;
  29. private static final float defaultTextSize = 16;
  30. private int defaultColor,selectedColor;
  31. private TabClickListner mClickListner;
  32.  
  33. public TabItemView(Context context) {
  34. this(context, null);
  35. }
  36.  
  37. public TabItemView(Context context, AttributeSet attrs) {
  38. this(context, attrs, 0);
  39. }
  40.  
  41. public TabItemView(Context context, AttributeSet attrs, int defStyle) {
  42. super(context, attrs, defStyle);
  43. this.mContext = context;
  44. init(attrs);
  45. addView();
  46. }
  47.  
  48. private void init(AttributeSet attrs){
  49. this.setOnClickListener(this);
  50. TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TabItemView);
  51. logoBackResourceId = ta.getResourceId(R.styleable.TabItemView_contentLogoBack, -1);
  52. textColor = ta.getColor(R.styleable.TabItemView_contentTextColor, getResources().getColor(android.R.color.black));
  53. textSize = ta.getDimensionPixelSize(R.styleable.TabItemView_contentTextSize, PhoneUtils.dp2px(mContext, defaultTextSize));
  54. textString = ta.getString(R.styleable.TabItemView_contentTextString);
  55. contentLogoSize = ta.getDimensionPixelSize(R.styleable.TabItemView_contentLogoSize, LayoutParams.WRAP_CONTENT);
  56. ta.recycle();
  57. defaultColor = mContext.getResources().getColor(R.color.textcolor_black_b3);
  58. selectedColor = mContext.getResources().getColor(R.color.textcolor_red_d);
  59. }
  60.  
  61. private void addView(){
  62. contentLogo = new ImageView(mContext);
  63. contentLogo.setFocusable(false);
  64. contentLogo.setClickable(false);
  65. LayoutParams logoParams = new LayoutParams(contentLogoSize,contentLogoSize);
  66. contentLogo.setLayoutParams(logoParams);
  67. if(logoBackResourceId != -1){
  68. contentLogo.setBackgroundResource(logoBackResourceId);
  69. }else{
  70. throw new InflateException("未设置填充图片资源");
  71. }
  72.  
  73. this.addView(contentLogo);
  74.  
  75. if(!TextUtils.isEmpty(textString)){
  76. contentText = new TextView(mContext);
  77. contentText.setFocusable(false);
  78. contentText.setClickable(false);
  79. LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  80. textParams.topMargin = PhoneUtils.dp2px(mContext,3);
  81. contentText.setLayoutParams(textParams);
  82. contentText.setTextColor(textColor);
  83. contentText.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
  84. contentText.setText(textString);
  85. this.addView(contentText);
  86. }
  87. }
  88.  
  89. @Override
  90. public void onClick(View v) {
  91. setTabSelected(true);
  92. if(mClickListner != null){
  93. mClickListner.onTabClick(this);
  94. }
  95. }
  96.  
  97. /**
  98. *设置点击监听事件
  99. */
  100. public void setTabClickListener(TabClickListner listner){
  101. this.mClickListner = listner;
  102. }
  103.  
  104. /**
  105. *设置填充图片资源
  106. */
  107. public void setContentLogoBack(int resourceId){
  108. contentLogo.setBackgroundResource(resourceId);
  109. }
  110.  
  111. /**
  112. *设置填充文字
  113. */
  114. public void setContentTextString(String text){
  115. if(contentText != null){
  116. contentText.setText(text);
  117. }
  118. }
  119.  
  120. /**
  121. *设置选中状态
  122. */
  123. public void setTabSelected(boolean enable){
  124. if(contentLogo != null){
  125. contentLogo.setSelected(enable);
  126. }
  127. if(contentText != null){
  128. if(enable){
  129. contentText.setTextColor(selectedColor);
  130. }else{
  131. contentText.setTextColor(defaultColor);
  132. }
  133. }
  134. }
  135.  
  136. public interface TabClickListner{
  137. void onTabClick(View view);
  138. }
  139.  
  140. }

TabClickListener:控件点击的回调接口

3.控件搞定,然后就是在布局里面的详细使用

在res/layout/中创建tab_layout.xml文件,详细代码例如以下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tabItem="http://schemas.android.com/apk/res/com.demo.dandy"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal"
  7. android:background="@color/background_bg7"
  8. android:paddingTop="@dimen/tab_padding"
  9. android:paddingBottom="@dimen/tab_padding">
  10.  
  11. <com.dandy.weights.TabItemView
  12. android:id="@+id/movie"
  13. style="@style/TabItemStyle"
  14. android:layout_width="0dp"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:gravity="center"
  18. android:orientation="vertical"
  19. tabItem:contentLogoBack="@drawable/selector_tab_movie"
  20. tabItem:contentTextString="@string/movie" />
  21.  
  22. <com.dandy.weights.TabItemView
  23. android:id="@+id/cinema"
  24. style="@style/TabItemStyle"
  25. android:layout_width="0dp"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="1"
  28. android:gravity="center"
  29. android:orientation="vertical"
  30. tabItem:contentLogoBack="@drawable/selector_tab_cinema"
  31. tabItem:contentTextString="@string/cinema" />
  32.  
  33. <com.dandy.weights.TabItemView
  34. android:id="@+id/community"
  35. style="@style/TabItemStyle"
  36. android:layout_width="0dp"
  37. android:layout_height="wrap_content"
  38. android:layout_weight="1"
  39. android:gravity="center"
  40. android:orientation="vertical"
  41. tabItem:contentLogoBack="@drawable/selector_tab_community"
  42. tabItem:contentTextString="@string/community" />
  43.  
  44. <com.dandy.weights.TabItemView
  45. android:id="@+id/mine"
  46. style="@style/TabItemStyle"
  47. android:layout_width="0dp"
  48. android:layout_height="wrap_content"
  49. android:layout_weight="1"
  50. android:gravity="center"
  51. android:orientation="vertical"
  52. tabItem:contentLogoBack="@drawable/selector_tab_mine"
  53. tabItem:contentTextString="@string/mine" />
  54.  
  55. </LinearLayout>

代码其中:xmlns:tabItem="http://schemas.android.com/apk/res/com.demo.dandy"。这段代码是关联你自己定义属性的。其中com.demo.dandy是指你应用的包名,tabItem是引用名

事实上就是拷贝xmlns:android="http://schemas.android.com/apk/res/android,替换一下android就ok了。

详细流程是:在构造函数中。获取TypedArray属性集合,然后获取所需的各个属性值,再通过动态加入控件ImageView和TextView,而且把对应定义的属性赋值给它们。

执行截图例如以下:

源码下载链接

android:自己定义组合控件Weight(高仿猫眼底部菜单条)的更多相关文章

  1. android 自己定义组合控件

    自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...

  2. [Android学习笔记]组合控件的使用

    组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...

  3. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  4. Android Studio自定义组合控件

    在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...

  5. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  6. android自己定义开关控件

    近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图:   waterma ...

  7. 撸一个Android高性能日历控件,高仿魅族

    Android原生的CalendarView根本无法满足我们日常开发的需要,在开发吾记APP的过程中,我觉得需要来一款高性能且美观简洁的日历控件,觉得魅族的日历风格十分适合,于是打算撸一款. gith ...

  8. android开发 获取父控件的高宽

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(wi ...

  9. Android自己定义View之组合控件 ---- LED数字时钟

    先上图 LEDView效果如图所看到的. 之前看到一篇博客使用两个TextView实现了该效果.于是我想用自己定义控件的方式实现一个LEDView.使用时就可以直接使用该控件. 採用组合控件的方式,将 ...

随机推荐

  1. [原创]Faster R-CNN论文翻译

    Faster R-CNN论文翻译   Faster R-CNN是互怼完了的好基友一起合作出来的巅峰之作,本文翻译的比例比较小,主要因为本paper是前述paper的一个简单改进,方法清晰,想法自然.什 ...

  2. 用户需求与NABCD分析

    用户需求与NABCD分析 目录 项目简介 用户需求分析 调研途径 问卷情况说明 问卷反馈与分析 NABCD分析 Need 需求 Approach 途径 Benefit 好处 Competitors 竞 ...

  3. net core 使用tagHelper将 enum枚举类型转换为下拉列表select

    [HtmlTargetElement("enums")] //[HtmlTargetElement("enums", TagStructure = TagStr ...

  4. 基于SwiperJs的H5/移动端下拉刷新上拉加载更多的效果

    最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...

  5. 电商SEO

    大家都知道网站有SEO,电商也有SEO,今天陈晨就带大家来讲讲电商SEO的思路以及电商最重要的选品规划! 1. 选品是核心 2. 挖掘卖点是你走向成功必经之路 3. 产品定价策略---人群画像 4. ...

  6. Linux系列教程(二十四)——Linux的系统管理

    上篇博客介绍了Linux的服务管理,不管是以RPM包安装的服务,还是通过源码包安装的服务,万能启动服务的方法都可以通过 /绝对路径/启动脚本名 start .而通过 RPM 包安装的服务还可以通过 s ...

  7. 盘点一下立过的flag并立几个flag

    暑假前说了,要学opencv3,要看完冰火,要健身,要家教挣钱. 好样的,全都没落下. opencv3几乎是把80%的demo码了一遍. 冰火看完,还顺带学了一波知识,收获颇丰,搞到了马丁老爷子的几本 ...

  8. laravel 嵌套的渴求式加载

    今天在通过需求表A查询场地类型表B,然后通过表B的场地类型id去查询表C场地类型名的时候遇到了一个小的问题. 需求表A的字段:id.user_id .name等等: 中间表B的字段:id.appeal ...

  9. python中函数的参数解析

    python中函数的各种参数梳理: 1.形参:函数定义时传入的参数 2.实参:函数调用时传入的参数 (有形参必传实参,形参里自身特点可不传的,可传可不传) 3.缺省参数:不传为默认值,传了会覆盖(下面 ...

  10. 教我徒弟Android开发入门(一)

    前言: 这个系列的教程是为我徒弟准备的,也适合还不懂java但是想学android开发的小白们~ 本系列是在Android Studio的环境下运行,默认大家的开发环境都是配置好了的 没有配置好的同学 ...