写在前面的话:

  • Android的UI界面都是由View和ViewGroup及其派生类组合而成的。其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器,其本身也是从View派生出来的。

  • 作为容器的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
  • 有时,我们想用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 . 在布局文件中引用自定义的视图控件

"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"        />

注意:

在布局文件的设计模式下看到自定义的视图控件

3 . 设置自定义视图控件的属性
在values下创建attr.xml

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(安卓)ScrollView嵌套WebView出现大面积空白页解决方法
  2. Android(安卓)intent 属性详解(二)
  3. Android仿人人客户端(v5.7.1)——消息中心视图的实现
  4. Android学习之界面篇(五)animateLayoutChanges
  5. 【Android(安卓)UI】 Shape详解 (GradientDrawable)
  6. Android之Adapter(适配器)
  7. Android使用LayoutInflater动态加载布局和操作控件
  8. android按需加载你的界面
  9. Android文字跑马灯控件(文本自动滚动控件)

随机推荐

  1. socket连接
  2. android操作xml
  3. Using WebViews and JavaScript
  4. android有用网址
  5. Android应用程序消息处理机制(Looper、Han
  6. android 模拟器使用指导
  7. 2011.09.26——— android sample之Notep
  8. 设置listview中的item的颜色
  9. android camera
  10. android 进制转换 (高低位)