-
Notifications
You must be signed in to change notification settings - Fork 42
android(17): View三大属性参数:attrs、defStyleAttr、defStyleRes
clarkehe edited this page Aug 14, 2016
·
1 revision
标题中的3个字符串,是3个变量。最近使用了ListPopupWindow,需要定义样式,对这3个变量作用进行了深入的了解。首先看下ListPopupWindow的构造函数:
/**
* Create a new, empty popup window capable of displaying items from a ListAdapter.
* Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}.
*
* @param context Context used for contained views.
* @param attrs Attributes from inflating parent views used to style the popup.
* @param defStyleAttr Style attribute to read for default styling of popup content.
* @param defStyleRes Style resource ID to use for default styling of popup content.
*/
public ListPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
mContext = context;
mHandler = new Handler(context.getMainLooper());
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListPopupWindow,
defStyleAttr, defStyleRes);
mDropDownHorizontalOffset = a.getDimensionPixelOffset(
R.styleable.ListPopupWindow_android_dropDownHorizontalOffset, 0);
mDropDownVerticalOffset = a.getDimensionPixelOffset(
R.styleable.ListPopupWindow_android_dropDownVerticalOffset, 0);
if (mDropDownVerticalOffset != 0) {
mDropDownVerticalOffsetSet = true;
}
a.recycle();
mPopup = new AppCompatPopupWindow(context, attrs, defStyleAttr);
mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
// Set the default layout direction to match the default locale one
final Locale locale = mContext.getResources().getConfiguration().locale;
mLayoutDirection = TextUtilsCompat.getLayoutDirectionFromLocale(locale);
}
看注释,这3个变量都能控制ListPopupWindow的样式,这3个值是什么关系呢?
我们把问题先放一放,我们来看下View的构造函数,发现也有这3个变量。
/**
* Perform inflation from XML and apply a class-specific base style from a
* theme attribute or style resource. This constructor of View allows
* subclasses to use their own base style when they are inflating.
* <p>
* When determining the final value of a particular attribute, there are
* four inputs that come into play:
* <ol>
* <li>Any attribute values in the given AttributeSet.
* <li>The style resource specified in the AttributeSet (named "style").
* <li>The default style specified by <var>defStyleAttr</var>.
* <li>The default style specified by <var>defStyleRes</var>.
* <li>The base values in this theme.
* </ol>
* <p>
* Each of these inputs is considered in-order, with the first listed taking
* precedence over the following ones. In other words, if in the
* AttributeSet you have supplied <code><Button * textColor="#ff000000"></code>
* , then the button's text will <em>always</em> be black, regardless of
* what is specified in any of the styles.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies default values for
* the view. Can be 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme. Can be 0
* to not look for defaults.
* @see #View(Context, AttributeSet, int)
*/
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
this(context);
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);
....
看View构造函数的大段注释,可以大概明白:attrs、defStyleAttr、defStyleRes都是用来确定View的属性(比如宽度、高度、背景等)的值,但使用谁提供的属性值有一定的规则。
先说下这3个的单独用法。
attrs: AttributeSet属性集。
属性:
defStyleAttr: 默认样式属性。Android内置一些默认的样式属性。
主题:
defStyleAttr: 默认样式资源。
样式: