美文网首页
自定义 View

自定义 View

作者: 涛涛123759 | 来源:发表于2017-06-01 14:21 被阅读14次
       public class CustomTextView extends TextView {
    
            private static final String TAG = CustomTextView.class.getSimpleName();
    
            public CustomTextView(Context context) {
                super(context);
            }
    
            public CustomTextView(Context context, AttributeSet attrs) {
                this(context, attrs, R.attr.CustomizeStyle);
            }
    
            public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
    
                /**
                 * AttributeSet set :XML 中定义的属性值,可能为 null;
                 *
                 * int[] attrs :目标属性值;
                 *
                 * dint defStyleAttr :在当前主题中有一个引用指向样式文件,这个样式文件将 TypedArray
                 * 设置默认值。如果此参数为 0 则表示不进行默认值设置。
                 *
                 * int defStyleRes :默认的样式资源文件,只有当 defStyleAttr 为 0 或者无法在对应的主题下找到资源文件时才起作用。
                 * 如果此参数为 0 则表示不进行默认设置。
                 */
                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView , defStyle, R.style.DefaultCustomizeStyle);
        //        TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs,  R.styleable.CustomTextView , R.attr.CustomizeStyle, R.style.DefaultCustomizeStyle);
                String one = a.getString(R.styleable.Customize_attr_one);
                String two = a.getString(R.styleable.Customize_attr_two);
                String three = a.getString(R.styleable.Customize_attr_three);
                String four = a.getString(R.styleable.Customize_attr_four);
                Log.i(TAG, "one:" + one);
                Log.i(TAG, "two:" + two);
                Log.i(TAG, "three:" + three);
                Log.i(TAG, "four:" + four);
                a.recycle();
            }
        }
    

    attrs.xml文件

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
    
            <declare-styleable name="CustomTextView">
                <attr name="attr_one" format="string" />
                <attr name="attr_two" format="string" />
                <attr name="attr_three" format="string" />
                <attr name="attr_four" format="string" />
            </declare-styleable>
    
            <attr name="CustomizeStyle" format="reference" />
    
        </resources>
    

    styles.xml文件

        <resources>
    
            <style name="AppBaseTheme" parent="android:Theme.Light">
            </style>
    
            <!-- Application theme. -->
            <style name="AppTheme" parent="AppBaseTheme">
    
                <!-- All customizations that are NOT specific to a particular API-level can go here. -->
                <item name="attr_one">attr one from theme</item>
                <item name="attr_two">attr two from theme</item>
                <item name="attr_three">attr three from theme</item>
                <item name="CustomizeStyle">@style/CustomizeStyleInTheme</item>
            </style>
    
            <style name="CustomizeStyleInTheme">
                <item name="attr_one">attr one from theme reference</item>
                <item name="attr_two">attr two from theme reference</item>
                <item name="attr_three">attr three from theme reference</item>
            </style>
    
            <style name="ThroughStyle">
                <item name="attr_one">attr one from style</item>
                <item name="attr_two">attr two from style</item>
            </style>
        
            <style name="DefaultCustomizeStyle">
                <item name="attr_one">attr one from defalut style res</item>
                <item name="attr_two">attr two from defalut style res</item>
                <item name="attr_three">attr three from defalut style res</item>
            </style>
    
        </resources>
    

    在activity_main.xml中添加该试图

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ad="http://schemas.android.com/apk/res-auto" <!--自定义类型-->
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity" >
    
            <com.example.myapplication.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ad:attr_one="attr one in xml"
                style="@style/ThroughStyle"
                android:text="@string/hello_world" />
    
        </RelativeLayout>
    

    相关文章

      网友评论

          本文标题:自定义 View

          本文链接:https://www.haomeiwen.com/subject/roqefxtx.html