Android中自定义组件及自定义属性

有时候在做开发的时候,android提供给我们的视图并不能满足我们的要求,所以有时候我们需要自己创建自己的view

我们只需要将我们想要的继承于View,然后重写里面的方法就可以了。

package com.example.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setTextColor(Color.BLUE);// 将字体设置成蓝色
	}

}


然后我们只需要在Layout中使用这个view就可以了。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.myviewtest01
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

 

如果我们要自定义属性,就像android:layout_height="wrap_content"这种:

首先我们要学习declare-styleable,它是给自定义控件添加自定义属性时用的。

我们在res/values下建立一个myAttrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyTextView">
        <attr name="fontSize" format="dimension" />
    </declare-styleable>

</resources>


 

解释一下上面那些值的属性

Name=”MyTextView” 是在R.java文件生成对应的引用名。

<attr name="fontSize"format="dimension" />这个就是自定义的属性名称。在R.java中会生成对应的名字,这个地方是MyTextView_fontSize.

后面的format是定义的你的变量的属性。如果你想学习有关他的更多详细信息,请转向:http://blog.csdn.net/lihengfang/article/details/8290754

 

在布局文件中我们就可以直接这样使用:

 

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:mytextview="http://schemas.android.com/apk/res/com.example.myviewtest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.myviewtest.MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mytextview:fontSize="20dp"
        android:text="text size = 20dp" >
    </com.example.myviewtest.MyTextView>
    <com.example.myviewtest.MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mytextview:fontSize="15dp"
        android:text="text size=15dp" >
    </com.example.myviewtest.MyTextView>

</LinearLayout>


其中xmlns:mytextview=http://schemas.android.com/apk/res/com.examp"中的地址是指向R.java所在的目录,我开始也不知道这个是干什么的,但是实验了一次,我就知道了。我开始以为那个是指向我们自定义的那个类,后面才发现,我错了。

我们自定义的View中,我们要获取给定的属性值,如下代码:

package com.example.myviewtest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

	public MyTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyTextView);
		String name = ta.getString(R.styleable.MyTextView_fontSize);
		System.out.println("name=" + name);
		this.setTextSize(ta.getDimension(R.styleable.MyTextView_fontSize, 10));
	}

	public MyTextView(Context context) {
		super(context);
		
	}

	

}


 

我的源码下载地址:

http://download.csdn.net/detail/lovecluo/5170841

http://download.csdn.net/detail/lovecluo/5170832 

 

 

 

 
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android开发,你可以根据自己的需求和设计,通过自定义组件来扩展和定制应用程序的用户界面。下面是一些常见的自定义组件的示例: 1. 自定义View:你可以继承`View`或`ViewGroup`类,通过重写`onDraw()`方法来自定义绘制视图的外观和行为。这样你可以创建各种自定义的图形、动画或交互效果。 2. 自定义布局:你可以继承`ViewGroup`类,通过重写`onLayout()`方法来自定义布局的方式。这样你可以创建特定的布局,如流式布局、瀑布流布局等。 3. 自定义控件:你可以继承现有的Android控件(如`TextView`、`Button`等),通过重写方法或添加属性和行为来定制控件。这样你可以创建具有特定功能或样式的自定义控件。 4. 自定义动画:你可以使用Android提供的动画框架(如属性动画、补间动画等),自定义动画效果。你可以通过改变视图的位置、尺寸、透明度等属性来创建自己的动画效果。 5. 自定义绘图:你可以使用Canvas和Paint等API,通过绘制图形、文本、图像等来创建自定义绘图效果。你可以在`View`或`SurfaceView`上绘制自己的图形。 在自定义组件时,你需要注意以下几点: - 理解自定义组件的需求和目标,合理选择继承的类。 - 重写合适的方法,以实现所需的外观和行为。 - 处理用户交互事件,如点击、滑动等。 - 考虑组件的可重用性和可扩展性。 - 进行适当的性能优化,避免过度绘制或内存泄漏。 以上只是自定义组件的一些示例,实际上你可以根据自己的需求和创意,创建各种各样的自定义组件。希望这些信息对你有帮助!如果你有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值