Android自定义xml属性

移动开发 Android
我们平常在xml文件里面定义,设置控件属性,android:text android:size这类的,有木有可以自定义的呢 答案是肯定的。android这个做的还是比较人性化的,可以自定义属性,自定义实现风格,主题,之类的。

Android 自定义组件

Android 提供了非常精致的和非常强大的组件化模型,能够更加方便的构建UI,这些UI组件都是基于基本的layout类:View 和 ViewGroup。

部分能够用的widgets包括:Button,TextView,EditText,ListView,CheckBox,RadioButton,Gallery,Spinner,和一些比较特殊用途的widgets(AutoCompleteTextViewImageSwitcher, and TextSwitcher.)

布局组件有LinearLayoutFrameLayoutRelativeLayout,absoluteLayout,TabelLayout

如果预定义的widgets和布局组件都不符合您的需求,那就需要创建属于自己的view,如果只是需要对已有的widget和layout进行小部分的调整,那就可以通过重写部分一些方法来完成开发。

下面就举个例子讲解如何创建自定义的xml属性,以及如果使用。

1. 首先创建一个新的android application.

2. 创建属性
在res/values/ 下创建一个attr.xml 文件,定义好需要的attributes

<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <declare-styleable name="custom">   
        <attr name="text" format="string" />   
        <attr name="size" format="integer"/>   
        <attr name="color" format="reference|color"/>   
    </declare-styleable>   
</resources>  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

 

3. 创建自定义的View

 

创建一个View, CustomView 继承自View(根据具体的情况,如果需求和已经存在的widget或者layout相差不大,就继承,重写一些方法)

package com.hualu.androidview;   
import android.content.Context;   
import android.content.res.TypedArray;   
import android.graphics.Canvas;   
import android.graphics.Paint;   
import android.util.AttributeSet;   
import android.view.View;   
public class CustomView extends View {   
    private  Paint p = null;   
    private String text =  null;   
    public CustomView(Context context) {   
        super(context);   
        initCustomView() ;   
    }   
    public CustomView(Context context, AttributeSet attrs){   
        super(context, attrs ) ;   
        initCustomView() ;   
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom) ;   
        int indexCount = a.getIndexCount() ;   
        for(int i = 0 ; i < indexCount ; i ++){   
            int index = a.getIndex(i) ;   
            switch (index) {   
            case R.styleable.custom_text:   
                text = a.getString(index) ;   
                break;   
            case R.styleable.custom_size:   
                p.setTextSize(a.getInt(index, 0));    
                break;   
            case R.styleable.custom_color:   
                p.setColor(a.getColor(index, 0xFF000000)) ;   
                break;   
            }   
        }   
        a.recycle() ;   
    }  
    void initCustomView(){   
         p = new Paint();   
         p.setAntiAlias(true);   
    } ;   
    @Override   
    protected void onDraw(Canvas canvas) {   
        super.onDraw(canvas);   
        canvas.drawText(text, 1010, p) ;   
    }   
}   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

4. 在layout的文件使用自定义的view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    xmlns:custom="http://schemas.android.com/apk/res/com.hualu.androidview"   
    xmlns:tools="http://schemas.android.com/tools"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent"   
    tools:context=".MainActivity" >   
    <TextView   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_centerHorizontal="true"   
        android:layout_centerVertical="true"   
        android:text="@string/hello_world" />   
    <com.hualu.androidview.CustomView   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        custom:text="custom view"   
        custom:color="#00FF00"   
        custom:size="18"   
        />   
</RelativeLayout> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

5. 运行应用

文章就到此结束,大家有什么疑问的,请留言,我会及时答复大家!谢谢~

责任编辑:闫佳明 来源: csdn
相关推荐

2016-12-26 15:25:59

Android自定义View

2022-09-21 14:42:03

JSProps属性

2016-11-16 21:55:55

源码分析自定义view androi

2009-08-04 13:35:16

ASP.NET自定义样

2011-08-09 17:16:56

CoreAnimati动画

2009-08-06 17:13:56

ASP.NET自定义控

2017-05-19 10:03:31

AndroidBaseAdapter实践

2016-04-12 10:07:55

AndroidViewList

2009-06-10 14:02:11

netbeans自定义项目

2010-01-18 15:43:35

VB.NET自定义属性

2011-03-17 09:45:01

Spring

2015-02-12 15:33:43

微信SDK

2017-05-18 12:36:16

android万能适配器列表视图

2010-02-07 14:02:16

Android 界面

2013-01-09 17:22:38

Android开发Camera

2015-02-12 15:38:26

微信SDK

2021-02-20 11:40:35

SpringBoot占位符开发技术

2021-03-09 15:23:45

鸿蒙HarmonyOS应用开发

2014-12-10 10:37:45

Android自定义布局

2015-02-11 17:49:35

Android源码自定义控件
点赞
收藏

51CTO技术栈公众号