1、统一的用户界面是能够使得应用程序更友好。要作到用户界面的统一,咱们就必须用到风格(style)和主题(theme)。
自定义一个View的方法步骤以下:
一、首先,在values文件夹下定义一个atts.xml的文件,描述自定义的控件的属性
在values/attrs.xml中:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="TestView">
- <attr name="textColor" format="color" />
- <attr name="textSize" format="dimension" />
- <attr name="imgBackground" format="integer" />
- <attr name="textPaddingLeft" format="dimension"/>
- <attr name="textPaddingTop" format="dimension"/>
- </declare-styleable>
- </resources>
二、其次,定义一个继承自View的类,如:TestView,使其实现View的方法
- package com.test.TestView;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.view.View;
-
- public class TestView extends View {
- private Paint mPaint;
- private Context mContext;
- private String mStr;
- public TestView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext = context;
- initMyView();
-
- TypedArray params = context.obtainStyledAttributes(attrs,R.styleable.MyView);
-
- int backgroudId = params.getResourceId(R.styleable.MyView_imgBackground, 0);
- if (backgroudId != 0)
- setBackgroundResource(backgroudId);
- int textColor = params.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);
- setTextColor(textColor);
- float textSize = params.getDimension(R.styleable.MyView_textSize, 36);
- setTextSize(textSize);
- float paddingLeft = params.getDimension( R.styleable.MyView_textPaddingLeft, 41);
- float paddingTop = params.getDimension(R.styleable.MyView_textPaddingTop, 21);
- setPaddings(paddingLeft, paddingTop);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- if (mStr != null) {
- canvas.drawText(mStr, 30, 60, mPaint);
- }
- }
- private void initMyView() {
- mPaint = new Paint();
- mPaint.setColor(Color.WHITE);
- }
- private void setTextColor(int textColor) {
- mPaint.setColor(0XFFAABBCC);
- }
- private void setTextSize(float textSize) {
- mPaint.setTextSize(textSize);
- }
- void setText(String text) {
- mStr = text;
- }
- private void setPaddings(float paddingLeft, float paddingTop) {
- setPadding((int) paddingLeft, (int) paddingTop, 0, 0);
- }
- }
三、而后,在Layout文件中应用该自定义的view,以下:
如在main.xml中
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <com.test.TestView.TestView
- android:id="@+id/myview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- app:textColor="#FFFFFFFF"
- app:textSize="40dip"
- app:textPaddingLeft="40dip"
- app:textPaddingTop="40dip"
- app:imgBackground="@drawable/bg" />
- </LinearLayout>
说明:
上述红色部分——xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"是首先声明命名空间。命名空间为app.路径是
http://schemas.android.com/apk/res/
这一部分是不变的,后面接的是R的路径:com.test.TestView.R。而后在自定义控件的xml描述中就能够这样使用app:value="true"。这样就实现了自定义控件的初始化赋值。
四、而后就是使用了,在本身的Activity 中
- public class TestActivity extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- TestView mTextView=(TestView)findViewById(R.id.TestView);
- mTextView.setText("自定义的View");
- }
- }
五、另外:java
如下内容转自:http://www.android123.com.cn/androidkaifa/591.htmlandroid
对于Android系统的自定义View可能你们都熟悉了,对于自定义View的属性添加,以及Android的Layout的命名空间问题,不少网友还不是很清楚,今天Android123一块儿再带你们温习一下canvas
- CwjView myView=new CwjView(context);
若是用于游戏或整个窗体的界面,咱们可能直接在onCreate中setContentView(myView); 固然若是是控件,咱们可能会须要从Layout的xml中声明,好比app
- <cn.com.android123.CwjView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
固然,咱们也能够直接从父类声明好比ide
- <View class="cn.com.android123.CwjView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
上面咱们仅用了父类View的两个属性,均来自android命名空间,而名称为layout_width或layout_height,咱们自定义的控件可能有更多的功能,好比编码
- <cn.com.android123.CwjView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- cwj:age="22"
- cwj:university="sjtu"
- cwj:city="shanghai"
- />
咱们能够看到上面的三个属性,是咱们自定义的。做为标准xml规范,可能还包含了相似 xmlns:android="http://schemas.android.com/apk/res/android" 这样的语句,对于定义完整的View,咱们的命名空间为cwj,这里能够写为 spa
xmlns:cwj=http://schemas.android.com/apk/res/cn.com.android123.cwjView 或 .net
xmlns:cwj=http://schemas.android.com/apk/res/android 均可以。3d
对于定义的cwj命名空间和age、university以及city的三个属性咱们如何定义呢? 在工程的res/values目录中咱们新建一个cwj_attr.xml文件,编码方式为utf-8是一个好习惯,内容以下
- <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <declare-styleable name="CwjView">
- <attr name="age" format="integer" />
- <attr name="city" format="string" />
- <attr name="university" format="string" />
- </declare-styleable>
- </resources>
这里咱们可能对format不是很熟悉,目前Android系统内置的格式类型有integer好比 ProgressBar的进度值,float好比RatingBar的值多是3.5颗星,boolean好比ToggleButton的是否勾 选,string好比TextView的text属性,固然除了咱们常见的基础类型外,Android的属性还有特殊的好比color是用于颜色属性的, 能够识别为#FF0000等类型,固然还有dimension的尺寸类型,好比23dip,15px,18sp的长度单位,还有一种特殊的为 reference,通常用于引用@+id/cwj @drawable/xxx这样的类型。
固然何时用reference呢? 咱们就以定义一个颜色为例子:
<attr name="red" format="color|reference" /> 这里咱们用了逻辑或的运算符,定义的红色是颜色类型的,同时能够被引用。固然,对于咱们自定义的类中,咱们须要使用一个名为 obtainStyledAttributes的方法来获取咱们的定义。在咱们自定义View的构造方法(Context context, AttributeSet attrs)的重载类型中能够用
- public CwjView(Context context, AttributeSet attrs) {
- super(context, attrs);
- TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.cwj_attr);
- mAge = a.getInteger(R.styleable.CwjView_age, 22);
- mCity = a.getString(R.styleable.CwjView_city, "shanghai");
- mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");
- a.recycle();
-
- }
这样类的全局成员变量 mAge、mCity就获取了咱们须要的内容,固然根据layout中的数值咱们自定义的CwjView须要动态的处理一些数据的状况,能够使用AttributeSet类的getAttributeResourceValue方法获取。
- public CwjView(Context context, AttributeSet attrs){
- super(context, attrs);
- resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "age", 100);
- resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView", "city", "shanghai");
-
以上两种方法中,参数的最后一个数值为默认的,若是您有不明白的地方能够来函到 android123@163.com 咱们会在第一时间回复。