android view使用方法,Android View构造方法第三参数使用方法详解
我们都知道,在Android中要使用一个View,一般会有两种方式:
在XML文件中配置;
直接在代码中new一个View的对象。
我们今天讨论的内容就是围绕着View的构造方法的。
1、实例
首先我们先来看一个例子。
新建一个工程,layout文件如下:
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="(Context, AttributeSet)" />
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.three_button_layout);
Button btn1 = new Button(this);
btn1.setText("(Context)");
Button btn2 = new Button(this, null, 0);
btn2.setText("(Context, AttributeSet, int)");
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.addView(btn1);
layout.addView(btn2);
}
在layout文件中有一个Button,然后在代码中new了两个Button,并且添加到layout文件中,显示结果如下:
Demo截图显示
很显然,前面两个Button样式是一样的,并且默认可以点击,第3个Button就有点奇怪了,而且还无法点击。为什么会出现这种现象呢?这就是这篇文章要说明的问题了。
View的构造函数
要想理解上面的问题,我们必须先得了解View的构造函数。默认情况下,View有3个构造函数,函数原型如下:
/**
* 在Code中实例化一个View就会调用这个构造函数
* Simple constructor to use when creating a view from code.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public View(Context context);
/**
* 在xml中定义会调用这个构造函数
* Constructor that is called when inflating a view from XML. This is called
* when a view is being constructed from an XML file
*/
public View(Context context, AttributeSet attrs);
public View(Context context, AttributeSet attrs, int defStyle);
如果要在代码中new一个View对象,我们一般会使用第一个构造函数。
如果是在XML文件中声明的View,系统会默认调用第二个构造函数。
而对于第三个构造函数,我们在自己的代码中一般都没有去调用它。
在上面的例子中,btn2这个Button正是采用的第三种构造方法创建出来的,结果导致了很奇怪的结果。既然是用Button做的例子,我们来看下Button的源码(Button的源码可以说是所有Android自带控件中最简单的了吧):
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
我们可以看到,整个类中仅仅只有3个构造方法,但是它继承自TextView,所以它的各种方法都是在TextView中实现的。然而,我们平时看到的TextView和Button还是有很多地方不同的,那是什么地方导致的这些差异呢?
显然,除了第二个构造方法中的com.android.internal.R.attr.buttonStyle,不可能有其他地方来区分TextView和Button了。而这里第二个构造方法调用了第三个构造方法,第三个构造比第二个构造方法多了一个int类型的参数。这就是关键所在了。
View构造方法中的第三个参数。
我们来看一下第三个构造方法的官方文档注释:
Perform inflation from XML and apply a class-specific base style. This constructor of View allows subclasses to use their own base style when they are inflating. For example, a Button class's constructor would call this version of the super class constructor and supply R.attr.buttonStyle for defStyle; this allows the theme's button style to modify all of the base view attributes (in particular its background) as well as the Button class's attributes.
对第三个参数的解释是:
An attribute in the current theme that contains a reference to a style resource to apply to this view. If 0, no default style will be applied.
它的大概意思就是,给View提供一个基本的style,如果我们没有对View设置某些属性,就使用这个style中的属性。
继续用Button来分析。
通过Button第3个构造方法的调用,我们来到TextView的构造方法中,当中有一句关键代码:
TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
接下来,我们分析一下obtainStyledAttributes方法。
obtainStyledAttributes
跟踪该方法,发现最终调用的是Resources.Theme类中的obtainStyledAttributes()方法,该方法里面主要是通过调用一个native方法来拿到控件的属性,放到TypedArray中。
public TypedArray obtainStyledAttributes(AttributeSet set,
@StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr, defStyleRes);
}
我们来仔细阅读一下obtainStyledAttributes()方法的官方文档
obtainStyledAttributes()方法的官方文档
set:在XML中明确写出了的属性集合。(比如android:layout_width、android:text="@string/hello_world"这些)
attrs:需要在上面的set集合中查询哪些内容。如果是自定义View,一般会把自定义的属性写在declare-styleable中,代表我们想查询这些自定义的属性值。
defStyleAttr:这是一个定义在attrs.xml文件中的attribute。这个值起作用需要两个条件:1. 值不为0;2. 在Theme中使用了(出现即可)。
defStyleRes:这是在styles.xml文件中定义的一个style。只有当defStyleAttr没有起作用,才会使用到这个值。
这还是一个比较模糊的概念,我们来看看系统里面是怎么使用这些值的。
首先找到frameworks\base\core\res\res\values目录下的attrs.xml、styles.xml、themes.xml三个文件,打开。
既然Button的构造方法中使用到了com.android.internal.R.attr.buttonStyle,我们就来看看这个attr。该attr位于attrs.xml中:
只是简单的定义了一个attr。
然后在哪里用到了它呢?看到themes.xml文件下,有这样一个style:
...
@android:style/Widget.Button
...
在这里用到了buttonStyle属性,它指向另外一个style,这个style在styles.xml文件下:
@android:drawable/btn_default
true
true
?android:attr/textAppearanceSmallInverse
@android:color/primary_text_light
center_vertical|center_horizontal
我们可以看到,这里面的属性都是用来配置Button的。如果在XML文件中没有给Button配置背景、内容的位置等属性,就会默认使用这里的属性。当然这是在使用了defStyleAttr的情况才会出现的,这也解释了文章开头的例子中的奇怪现象了。
千万不要以为这样就万事大吉了,现在我们只是定义好了这些属性,并没有使用到它。那在哪里使用到的呢?注意上面的themes.xml中的那个style的名称为Theme,而在我们自己的工程中,在配置menifest文件的时候,给application或者activity设置的主题android:theme一般都是这个style的子类,所以也就这样使用到了defStyleAttr定义的属性了。至于是如何拿到这些属性的,我想是在obtainStyledAttributes()方法中处理的,这里不需要过多追究。
还有一个defStyleRes参数,我们可以发现在TextView、ImageView等控件中,这个值传的都是0,也就是不使用它。它的作用就像是一个替补,当defStyleAttr不起作用的时候它就上场,因为它也是一个style,这个参数是怎么起作用的在下面的实例中有提到。
实例
上面的都是理论,我们接下来用一个例子来实践一下。
首先创建一个attrs.xml文件:(如果还不会自定义View属性的,请参考
注意,这里即使将customViewStyle属性写在declare-styleable里,最终效果也一样。
定义style。
首先定义我们的defStyleAttr属性(在本项目中是customViewStyle属性)需要用到的style(位于styles.xml文件中):
attr3 from custom_view_style
attr4 from custom_view_style
然后定义一个在xml布局文件中需要用到的style(位于styles.xml文件中):
attr2 from xml_style
attr3 from xml_style
自定义一个简单的View:
public class CustomView extends View {
static final String LOG_TAG = "CustomView";
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.customViewStyle);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyle, 0);
Log.d(LOG_TAG, "attr1 => " + array.getString(R.styleable.CustomView_attr1));
Log.d(LOG_TAG, "attr2 => " + array.getString(R.styleable.CustomView_attr2));
Log.d(LOG_TAG, "attr3 => " + array.getString(R.styleable.CustomView_attr3));
Log.d(LOG_TAG, "attr4 => " + array.getString(R.styleable.CustomView_attr4));
Log.d(LOG_TAG, "attr5 => " + array.getString(R.styleable.CustomView_attr5));
Log.d(LOG_TAG, "attr6 => " + array.getString(R.styleable.CustomView_attr6));
}
}
注意这里用到了R.attr.customViewStyle。为了使它生效,需要在当初工程的theme中设置它的值(位于styles.xml文件中):
@style/custom_view_style
这里就用到了我们上面定义的custom_view_style这个style。
运行结果:
运行结果
分析:
attr1只在xml布局文件中设置,所以值为attr1 from xml。
attr2在xml布局文件和xml style中都设置了,取值为布局文件中设置的值,所以为attr2 from xml。
attr3没有在xml布局文件中设置,但是在xml style和defStyleAttr定义的style中设置了,取xml style中的值,所以值为attr3 from xml_style。
attr4只在defStyleAttr定义的style中设置了,所以值为attr4 from custom_view_style。
attr5和attr6没有在任何地方设置值,所以为null。
这也证实了前面所得出的顺序是正确的。
我们再来测试一下defStyleRes这个参数,它是一个style,所以添加一个style(位于styles.xml文件中):
attr4 from default_view_style
attr5 from default_view_style
然后还需要修改CustomView中的第16行,为下面一行:
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyle, R.style.default_view_style);
运行结果:
运行结果
咦,为什么结果和上面一样呢?
我们看到官方文档中对obtainStyledAttributes()方法的defStyleRes参数解释是这样的:
A resource identifier of a style resource that supplies default values for the TypedArray, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.
也就是说,当defStyleAttr这个参数定义为0(即不使用这个参数),或者是在theme中找不到defStyleAttr这个属性时(即使在theme中的配置是这样的:@null,也代表找到了defStyleAttr属性,defStyleRes参数也不会生效),defStyleRes参数才会生效。
所以我们修改CustomView为下面内容(或者是去掉theme中对customViewStyle的使用):
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, R.style.default_view_style);
运行结果:
运行结果
由于defStyleAttr已经失效,所以attr4和attr5都是从default_view_style中获取到的值。
我们知道,在theme所在的style中也可以设置属性,如下:
@style/custom_view_style
attr5 from AppTheme
attr6 from AppTheme
运行结果:
运行结果
attr1~attr4不用说了。
attr5在default style和theme下都定义了,取default style下的值,所以为attr5 from default_view-style。
attr6只在theme下定义了,所以取值为attr6 from AppTheme。
注意,如果将CustomView中重新改成下面的内容(即使customViewStyle生效):
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyle, 0);
这时,default style是失效了的,那么在theme中设置的值会不会生效呢?
看运行结果:
运行结果
attr5在default style和theme下都定义了,但default style失效了,这里并没有因为customViewStyle是有效的而忽略theme中设置的值,所以为attr5 from AppTheme。
attr6只在theme下定义了,同样没有因为customViewStyle是有效的而忽略theme中设置的值,所以取值为attr6 from AppTheme。
这里和default style的取值形式有一点点不同。
总结
View中的属性有多处地方可以设置值,这个优先级是:
1、直接在XML布局文件中设置的值优先级最高,如果这里设置了值,就不会去取其他地方的值了。
2、XML布局文件中有一个叫“style”的属性,它指向一个style,在这个style中设置的属性值优先级次之。
3、如果上面两个地方都没有设置值,那么就会根据View带三个参数的构造方法中的第三个参数attribute指向的style设置值,前提是这个attribute的值不为0。
4、如果上面的attribute设置为0了,我们就根据obtainStyledAttributes()方法中的最后一个参数指向的style来设置值。
5、如果仍然没有设置到值,就会用theme中直接设置的属性值,而不会去管第3步和第4步中是否设置了值。
必须要注意:要想让View构造方法的第三个参数生效,必须让它出现在我们自己的Application或者Activity的android:theme所指向的style中。设置Activity的theme一样可以。
参考文章:
android view使用方法,Android View构造方法第三参数使用方法详解相关推荐
- Android插件化开发之动态加载三个关键问题详解
本文摘选自任玉刚著<Android开发艺术探索>,介绍了Android插件化技术的原理和三个关键问题,并给出了作者自己发起的开源插件化框架. 动态加载技术(也叫插件化技术)在技术驱动型的公 ...
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
李华明Himi 原创,转载务必在明显处注明: 转载自 [黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/327.html 很多童鞋说 ...
- C语言中三个数比较大小详解——三种方法
C语言中三个数比较大小详解--三种方法 方法一:if-else法 方法二:函数法 方法三:三目运算符法 C语言中比较三个数的大小有很多方法,以下是我总结的三种方法: 首先我定义 int a = 1 ...
- 永磁同步电机电阻、电感、惯量、转矩系数、反电势系数、极对数、编码器零位等一系列的参数辩识方法,电流速度环pi参数整定方法
参数辩识 永磁同步电机电阻.电感.惯量.转矩系数.反电势系数.极对数.编码器零位等一系列的参数辩识方法,电流速度环pi参数整定方法,都是从实际项目中总结出来的,个人写成了文档,很详细,想学参数辩识的可 ...
- Android之定时器实现的几种方式和removeCallbacks失效问题详解
转载:http://blog.csdn.net/xiaanming/article/details/9011193 实现定时器有很多种方式,在这里我简单的介绍几种方式 实现定时器有很多种方式,在这里我 ...
- 【Android】Listview返回顶部,快速返回顶部的功能实现,详解代码。
2019独角兽企业重金招聘Python工程师标准>>> 作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://we ...
- 华为Android岗面经;群面+技术面+英语面+面试题详解
前言 昨天是我去华为面试的整整一天,早上9点开始出发,一直弄到下午5点半,终于完成了所有的测评和面试! 简单说一下,我面的是Android高级开发职位,一个华为的前辈内推的.目前5年半开发经验.面试之 ...
- android java 写文件操作_Android编程之文件的读写实例详解
本文实例分析了Android编程之文件的读写方法.分享给大家供大家参考,具体如下: Android的文件读写与JavaSE的文件读写相同,都是使用IO流.而且Android使用的正是JavaSE的IO ...
- Android简单登录界面,保存账号和密码(基础,详解)
一 问题描述: 制作一个简单的登录界面,并使用文件储存方式储存用户名和密码,在下次打开应用时自动获取上次储存的账户和密码 二 解题思路: 文件储存: 文件存储是Android中最基本的一种数据存储方式 ...
最新文章
- 8年面试官问到:数据库自增 ID 用完了会咋样?
- 排序算法复习之一趟快速排序算法:为什么说关键字所占的位置是多余的
- 第三方网站实现绑定微信登陆
- linux——rpm的详细说明
- 科大星云诗社动态20210504
- SpringBoot demo初始
- 增加网格_网格交易法(期货)
- ATL--创建简单的ATL之dll工程,添加“ATL简单对象”类的参数说明
- android 动画坐标,Android 动画之TranslateAnimation应用详解
- Eigen教程(3)之矩阵和向量的运算
- python表格模板_python 网站 使用表单和模板
- python - - 函数 - - 递归函数
- 首席架构师白鳝:运维的进阶与哲学之道
- 卫星地图上有没有UFO影像?
- 1.30 fcntl函数
- 支付宝扫描二维码支付
- CSB专享实例部署操作手册
- Learning-based Practical Smartphone Eavesdropping with Built-in Accelerometer综述
- 服务器winsxs文件夹怎么清理工具,win7如何使用WinSxS工具安全删除WinSxS文件夹垃圾...
- 小小白学习c语言分享