前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >速读原著-Android应用开发入门教程(自定义的视图)

速读原著-Android应用开发入门教程(自定义的视图)

作者头像
cwl_java
发布2020-02-13 17:03:59
5050
发布2020-02-13 17:03:59
举报
文章被收录于专栏:cwl_Javacwl_Java

7.3 自定义的视图

自定义的 View 的含义是通过扩展的方法,实现一个扩展 android.view.View 类的类,这个类的本质也是一个控件,通过它可以直接构建 UI。

参考示例程序:CustomView(ApiDemo=>Views=>CustomView ) 源代码:com/example/android/apis/view/CustomView1.java

代码语言:javascript
复制
com/example/android/apis/view/LabelView.java

布局文件:custom_view_1.xml CustomView 程序的运行结果如图所示:

在这里插入图片描述
在这里插入图片描述

布局文件 custom_view_1.xml 的如下所示:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <com.example.android.apis.view.LabelView 
 android:background="@drawable/red" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 app:text="Red"/> 
 <com.example.android.apis.view.LabelView 
 android:background="@drawable/blue" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 app:text="Blue" app:textSize="20dp"/> 
 <com.example.android.apis.view.LabelView 
 android:background="@drawable/green" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 app:text="Green" app:textColor="#ffffffff" /> 
</LinearLayout>

这里使用的标签 com.example.android.apis.view.LabelView 不是 Android 框架层提供的 View 的一个子类,而是在自己的程序中实现的一个类。能在布局文件中使用的类,也都是 android.view.View 类的继承者。

这个 com.example.android.apis.view.LabelView,在源文件 LabView.java 中实现,其主要片段如下所示:

代码语言:javascript
复制
    public class LabelView extends View {
        public LabelView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initLabelView();
            TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.LabelView);
            CharSequence s = a.getString(R.styleable.LabelView_text);
            if (s != null) {
                setText(s.toString());
            }
            setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
            int textSize =
                    a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
            if (textSize > 0) {
                setTextSize(textSize);
            }
            a.recycle();
        }
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawText(mText, getPaddingLeft(),
                    getPaddingTop() - mAscent, mTextPaint);
        }
    }

在 LabelView 的构造函数中,通过 context.obtainStyledAttributes 获得 LabelView 所特有的几个属性。R.styleable.LabelView 这些内容在 res/values/的 attrs.xml 文件中进行了定义,内容如下所示:

代码语言:javascript
复制
 <declare-styleable name="LabelView"> 
	 <attr name="text" format="string" /> 
	 <attr name="textColor" format="color" /> 
	 <attr name="textSize" format="dimension" /> 
 </declare-styleable>

根据 LabView.java 实现的类名称,这样自定义的控件也可以在布局文件中使用,使用标签与类名相一致。R.styleable.LabelView_textR.styleable.LabelView_textColorR.styleable.LabelView_textSize 是在源代码中使用的属性,它们与引用 LabelView 的布局文件中的 app:text,app:textColor 和 app:textSize 等几个属性相对应。作为公共的属性,LabelView 在实现上也应该具有公共的函数来设置这几个属性。这些函数如下所示:

代码语言:javascript
复制
    public void setText(String text) {
        mText = text;
        requestLayout();
        invalidate();
    }
    public void setTextSize(int size) {
        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();
    }
    public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();
    }

以上的几个函数和几个 XML 中的属性对应的,如果没有他们,这些属性就只能在 XML 文件中指定,而不能在 JAVA 源文件中设置。

在 Android 的应用程序层,可以通过扩展 android.view.View 或者它的扩展者来实现自己的 View。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 7.3 自定义的视图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档