美文网首页
自定义View鉴赏

自定义View鉴赏

作者: heiheiwanne | 来源:发表于2016-10-14 16:02 被阅读25次

首先自定义view需要重写onDraw方法,处理wrap_content/padding 的问题

加入自定义参数:

  • attrs.xml中
<resources>
    <declare-styleable name="MyHorizontalListView">
        <attr name="myDividerWidth" format="dimension" />
        <attr name="android:divider" />
        <attr name="android:requiresFadingEdge" />
        <attr name="android:fadingEdgeLength" />
    </declare-styleable>
</resources>
  • 代码中获取数值
private void retrieveXmlConfiguration(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyHorizontalListView);

            // Get the provided drawable from the XML
            final Drawable d = a.getDrawable(R.styleable.MyHorizontalListView_android_divider);
            if (d != null) {
                // If a drawable is provided to use as the divider then use its intrinsic width for the divider width
                setDivider(d);
            }

            // If a width is explicitly specified then use that width
            final int dividerWidth = a.getDimensionPixelSize(R.styleable.MyHorizontalListView_myDividerWidth, 0);
            if (dividerWidth != 0) {
                setDividerWidth(dividerWidth);
            }

            a.recycle();
        }
    }
  • 使用自定义View
 <com.xxtoutiao.xxtt.view.MyHorizontalListView
                android:id="@+id/home_myHorizontalListView"
                android:layout_width="match_parent"
                android:layout_height="44dp"
                android:layout_toRightOf="@+id/home_avatar"
                android:layout_toLeftOf="@+id/set_channel"
                app:tabMode="scrollable" />

xml中:xmlns:app=http://schemas.android.com/apk/res-auto

app:是自定义属性的前缀,当然可以换其他名字,但是CircleView中使用自定义属性了,比如:app:circle_color= “@color/light_green”

也可以写为
xmlns:app=http://schemas.android.com/apk/com.xxtoutiao.xxtt

自定义view不要忘记wrap_content时的处理

相关文章

  • 自定义View鉴赏

    首先自定义view需要重写onDraw方法,处理wrap_content/padding 的问题 加入自定义参数:...

  • Android View(转)

    自定义View的原理自定义View基础 - 最易懂的自定义View原理系列自定义View Measure过程 - ...

  • 自定义View系列

    自定义View1---知识储备自定义View2---View Measure过程自定义View3---View L...

  • 自定义View5---完整的自定义View

    移步自定义View系列 1.自定义view的分类自定义单一view(不含子view)继承view继承特定view如...

  • 自定义View

    自定义View系列文章 自定义View view向上滚动

  • 自定义View(一) 自定义View的概述

    不怕跌倒,所以飞翔 自定义View概述 1.自定义View分类 自定义View 直接继承View主要是绘制 自定义...

  • Android自定义View

    Android自定义View 参考:从此再有不愁自定义View——Android自定义view详解android ...

  • 自定义view

    Android自定义View 为什么要自定义View自定义View的基本方法 自定义View的最基本的三个方法分别...

  • Android 之 自定义View全解

    本文主要有以下内容: 自定义View的分类 自定义View的注意事项 自定义View的不同实现 自定义View使其...

  • 高级Android工程师进阶系列文章汇总

    自定义View HenCoder(朱凯)自定义View系列 自定义view总结 Android 样式的开发 And...

网友评论

      本文标题:自定义View鉴赏

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