给自定义View添加xml属性
来源:互联网 发布:王国纪元建造实力算法 编辑:程序博客网 时间:2024/05/19 18:42
笔者之前已经写过了一些自定义View的文章,在此对其也就不从头说起了,如有兴趣的读者可以看一下笔者的前两篇文章。
android 自定义view的使用(最佳demo——返回标题栏)
android 自定义控件(底部icon点击效果)
笔者之前的文章中仅仅介绍了如何使用自定义View以及为什么要使用自定义View等等,但是在实际操作中,我们还是希望自定义View之后,直接能够在xml中就对其进行操作,如下图:
那么如何操作呢?主要是三个步骤:
1、自定义属性名称
2、将属性名称与控件关联
3、从第三方命名空间获取到自定义属性名称
主要代码:
1、自定义属性名称
首先要在values文件中创建一个xml文件,并且在其中写上你需要的自定义属性的名称以及类型。
atts.xml中代码如下:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyTitle"> <attr name="textColor" format="color"/> <attr name="titleText" format="string"/> <attr name="leftText" format="string"/> <attr name="rightText" format="string"/> </declare-styleable></resources>
2、将属性名称与控件关联
此点比较简单,直接看代码:
MyView.java
package com.example.double2.viewxmltest;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.TextView;/** * 项目名称:ViewXmlTest * 创建人:Double2号 * 创建时间:2016/8/4 10:23 * 修改备注: */public class MyView extends LinearLayout { private int colorText; private String textLeft; private String textTitle; private String textRight; private TextView tvLeft; private TextView tvTitle; private TextView tvRight; public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //从xml的属性中获取到字体颜色与string TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyTitle); colorText=ta.getColor(R.styleable.MyTitle_textColor,Color.BLACK); textLeft=ta.getString(R.styleable.MyTitle_leftText); textTitle=ta.getString(R.styleable.MyTitle_titleText); textRight=ta.getString(R.styleable.MyTitle_rightText); ta.recycle(); //获取到控件 //加载布局文件,与setContentView()效果一样 LayoutInflater.from(context).inflate(R.layout.my_view, this); tvLeft=(TextView)findViewById(R.id.tv_left); tvTitle=(TextView)findViewById(R.id.tv_title); tvRight=(TextView)findViewById(R.id.tv_right); //将控件与设置的xml属性关联 tvLeft.setTextColor(colorText); tvLeft.setText(textLeft); tvTitle.setTextColor(colorText); tvTitle.setText(textTitle); tvRight.setTextColor(colorText); tvRight.setText(textRight); }}
my_view.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" tools:background="@android:color/holo_blue_dark"> <TextView android:id="@+id/tv_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" tools:text="left" tools:textColor="#fff"/> <TextView android:id="@+id/tv_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="23sp" tools:text="title" tools:textColor="#fff"/> <TextView android:id="@+id/tv_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" tools:text="right" tools:textColor="#fff"/></LinearLayout>
3、从第三方命名空间获取到自定义属性名称
此处要注意在activity_main.xml要申明第三方命名空间(在android studio中只需要用res-auto,在eclipse中就需要加上完整的包名,如下图)
注:my_view只是使用时的一个名称而已,后方的“http://schemas.android.com/apk/res-auto”才是真正有用的。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.double2.viewxmltest.MyView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_dark" my_view:leftText="Back" my_view:rightText="Go" my_view:textColor="#fff" my_view:titleText="MyViewTest" /></RelativeLayout>
最后附上源码:http://download.csdn.net/detail/double2hao/9594621
3 0
- 给自定义View添加xml属性
- android 自定义view添加自定义xml属性
- 【Tech-Android-View】给自定义view添加自定义属性
- 【Tech-Android-View】给自定义view添加自定义属性
- Android自定义View 属性添加
- 为自定义View添加属性
- 随手——给自定义view的自定义属性添加注释
- 自定义View添加自定义属性详细
- 自定义View之添加自定义属性
- Android自定义View 自定义xml属性设置
- Android给自定义控件添加自定义属性
- 如何给自定义控件添加自定义属性
- 自定义view系列(3)--给自定义View添加点击事件
- Android 自定义View并添加属性介绍
- Android 自定义View并添加属性
- Android 自定义View并添加属性
- Android 自定义View并添加属性
- Android 自定义View并添加属性
- 获取本地图片或拍照并保存到本地
- ionic 常用命令
- JS:js和css本地缓存问题
- java map转bean
- Android中直播视频技术探究之---采集摄像头Camera视频源数据进行推流(采用金山云SDK)
- 给自定义View添加xml属性
- git提交代码到github
- POJ 1789 Truck History (最小生成树---prim)
- 【算法】网易2017校园招聘笔试程序题(分田地)
- PAT-A 1024. Palindromic Number
- Makefile 使用总结
- MVVM从入门到了解
- Hibernate 多对多之拆分中间表增加有多个关系字段
- ubuntu sever 的一些常用操作