theme- 自定义控件属性

今天想要在一个控件中增加自己的一条属性,具体步骤如下

1.在frameworks/base/core/res/res/values/attr中注册属性

因为我们希望增加的属性是在AutoCompleteTextView中,所以我们添加如下

 <declare-styleable name="AutoCompleteTextView">
        <!-- Defines the marginright in the drop down menu. -->
        <attr name="dropDownListItemPaddingRight" format="dimension" />
        <!-- Defines the hint displayed in the drop down menu. -->
        <attr name="completionHint" format="string" />
        <!-- Defines the hint view displayed in the drop down menu. -->
        <attr name="completionHintView" format="reference" />
        <!-- Defines the number of characters that the user must type before
         completion suggestions are displayed in a drop down menu. -->
        <attr name="completionThreshold" format="integer" min="1" />
        <!-- Selector in a drop down list. -->
        <attr name="dropDownSelector" format="reference|color" />
        <!-- Amount of pixels by which the drop down should be offset vertically. -->
        <attr name="dropDownVerticalOffset" format="dimension" />
        <!-- Amount of pixels by which the drop down should be offset horizontally. -->
        <attr name="dropDownHorizontalOffset" format="dimension" />
        <!-- View to anchor the auto-complete dropdown to. If not specified, the text view itself
             is used. -->
        <attr name="dropDownAnchor" format="reference" />
 </declare-styleable name="AutoCompleteTextView">

2.代码中引用

主要是在AutoCompleteTextView.java

        TypedArray a =
            context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.AutoCompleteTextView, defStyle, 0);
mPopupMargnRight = (int) a.getDimension(R.styleable.AutoCompleteTextView_dropDownListItemPaddingRight, 0.0f);

3.style 中设定属性值

 <style name="Widget.Funui.AutoCompleteTextView" parent="Widget.AutoCompleteTextView">
        <item name="android:dropDownSelector">@android:drawable/list_selector_funui</item>
        <item name="android:popupBackground">@android:drawable/menu_dropdown_panel_funui</item>
        <item name="android:dropDownListItemPaddingRight">13dp</item>
    </style>
原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_edittext_150312134.html