写在前面的话:

  • Android的UI界面都是由View和ViewGroup及其派生类组合而成的。其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器,其本身也是从View派生出来的。
    Android--自定义视图控件(一)(Android Studio)_第1张图片

  • 作为容器的ViewGroup可以包含作为叶子节点的View,也可以包含作为更低层次的子ViewGroup,而子ViewGroup又可以包含下一层的叶子节点的View和ViewGroup。事实上,这种灵活的View层次结构可以形成非常复杂的UI布局,开发者可据此设计、开发非常精致的UI界面。

  • 一般来说,开发Android应用程序的UI界面都不会直接实用View和ViewGroup,而是使用这两大基类的派生类。

    View派生出的直接子类有:AnalogClock,ImageView,KeyboardView,ProgressBar,SurfaceView,TextView,ViewGroup,ViewStub  ViewGroup派生出的直接子类 有:AbsoluteLayout,FragmentBreadCrumbs,FrameLayout,LinearLayout,RelativeLayout,SlidingDrawer,AdapterView<T extends Adapter>
  • 有时,我们想用ImageView在默认情况下加载一幅图片,但是希望在点击该View时View变换出各种图像处理效果,这个时候直接使用 ImageView是不行的,此时我们可以重载ImageView,在新派生出的子控件中重载OnDraw等方法来实现我们的定制效果。这种派生出系统类 的子类方法我们通常称之为自定义控件。自定义控件可像标准View控件那样在XML及我们的Java文件中进行布局和实例化,但在布局时必须指出其完整的包名和类名。

一、自定义视图属性

说明:在Android Studio中无法识别“自定义属性xmlns”问题,如下:
这里写图片描述
解决办法:
这里写图片描述
即直接写res-auto而不是写包名。因为Gradle工具会自动识别,所以不用指定包名

  1. 继承View类
/** * 自定义视图控件 * Created by FanFF on 2016/2/27. */public class MyRect extends View {    // 资源属性的构造器,供资源解析器来访问(只要将相应的xml资源文件放到values即可)    public MyRect(Context context, AttributeSet attrs){        super(context, attrs);    }    public MyRect(Context context){        super(context);    }}

2 . 在布局文件中引用自定义的视图控件

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:fanff="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.fanff.myselfproperty.MainActivity">    <com.example.fanff.myselfproperty.MyRect        android:layout_width="100dp"        android:layout_height="100dp"        fanff:rect_color="#ff0000"        /></LinearLayout>

注意:
Android--自定义视图控件(一)(Android Studio)_第2张图片
在布局文件的设计模式下看到自定义的视图控件
Android--自定义视图控件(一)(Android Studio)_第3张图片

3 . 设置自定义视图控件的属性
在values下创建attr.xml
Android--自定义视图控件(一)(Android Studio)_第4张图片
attr.xml的代码如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyView">        <attr name="rect_color" format="color"/>    </declare-styleable></resources>

关于这些属性值的简要说明,参见http://blog.csdn.net/u014294166/article/details/50754890

4 . 自定义控件的设置

public class MyRect extends View {    // 资源属性的构造器,供资源解析器来访问(只要将xml资源文件放到values即可)    public MyRect(Context context, AttributeSet attrs){        super(context, attrs);        //获取配置属性        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);        // 设置默认值        int color = ta.getColor(R.styleable.MyView_rect_color, 0x00ff00);        setBackgroundColor(color);        ta.recycle();    }    public MyRect(Context context){        super(context);    }}

更多相关文章

  1. Android自定义控件的使用
  2. 如何动态获取Android系统属性
  3. 一个非常好用的自定义圆形控件
  4. Android之在父视图上面布局子视图
  5. ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView)
  6. Android属性动画实战教程中篇
  7. Android原生Switch控件滑块thumb卡住问题的解决方法
  8. View视图框架源码分析之一:android是如何创建一个view
  9. android控件的监听绑定方法

随机推荐

  1. android之CalendarView日历视图
  2. PC与Android设备通过USB建立通信连接
  3. Android帮助文档翻译——开发指南(十五)获
  4. 【android开发】解析xml文件①
  5. 桌面云的三种模式 VDI IDV VOI (笔记)
  6. Android imageview图片缩放实现
  7. 如何使用自己的makefile编译android ndk
  8. android Dependencies ,Private Libraries
  9. Android Layout的layout_height等属性为
  10. Android对Linux内核的改动你知道多少?