Andriod: 在xml布局中使用自定义属性
来源:互联网 发布:淘宝网买男鞋 编辑:程序博客网 时间:2024/06/06 03:49
Andriod: 在xml布局中使用自定义属性
1、自定义View的属性,首先在res/values/ 下建立一个attr.xml , 在里面定义我们的属性和声明我们的整个样式。
`<?xml version="1.0" encoding="utf-8"?><resources><attr name="relation"> <enum name="icon_left" value="0" /> <enum name="icon_right" value="1" /> <enum name="icon_above" value="2" /> <enum name="icon_below" value="3" /></attr><declare-styleable name="IconText"> <attr name="relation" /> <attr name="icon" format="reference" /> <attr name="text" format="string" /> <attr name="text_size" format="dimension" /> <attr name="text_color" format="integer" /> <attr name="space" format="dimension" /></declare-styleable></resources>`
解释
属性relation有4种可选值:icon_left, icon_right, icon_above,icon_below.属性icon的可选值为引用: 例如:"@/drawbable/icon".属性text的可选值为string, 例如: "Hello world", 也可是string的引用"@string/hello".属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等.属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white".format还可以指定其他的类型比如;reference 表示引用,参考某一资源IDstring 表示字符串color 表示颜色值dimension 表示尺寸值boolean 表示布尔值integer 表示整型值float 表示浮点值fraction 表示百分数enum 表示枚举值flag 表示位运算
2、在layout布局文件中使用这个自定布局及其属性
` <com.example.myview_day02.MyView xmlns:IconText="http://schemas.android.com/apk/res/com.example.myview_day02" IconText:relation="icon_left" IconText:text="hi,how are you!" IconText:text_size="12sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/>`
解释
我们一般使用如下的值xmlns:icontext=""http://schemas.android.com/apk/res/<你的应用程序的包名>"icontext这个是我们自定属性的名字
3、TypedArray详解
TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合实例代码package com.example.myview_day02;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * Created by Administrator on 2016/7/5 0005. */public class MyView extends LinearLayout { private final static String TAG = "IconTextView"; private final int ICON_LEFT = 0; private final int ICON_RIGHT = 1; private final int ICON_ABOVE = 2; private final int ICON_BELOW = 3; private TextView mTextView; private ImageView mImageView; private int mRelation = ICON_LEFT; private String mText = ""; private int mIconId; private float mTextSize; private int mSpace; public MyView(Context context, AttributeSet attrs){ super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconText); mRelation = a.getInt(R.styleable.IconText_relation, ICON_LEFT); Log.d(TAG,"mRelation: "+mRelation); mText = a.getString(R.styleable.IconText_text); Log.d(TAG,"mText: "+mText); mTextSize = a.getDimensionPixelSize(R.styleable.IconText_text_size, 12); Log.d(TAG,"mTextSize: "+mTextSize); mSpace = a.getDimensionPixelSize(R.styleable.IconText_space, 5); Log.d(TAG,"mSpace: "+mSpace); mIconId = a.getResourceId(R.styleable.IconText_ico, R.drawable.haha); Log.d(TAG,"mIconId: "+mIconId); a.recycle(); mTextView = new TextView(context); mTextView.setText(mText); mTextView.setTextSize(mTextSize); mImageView = new ImageView(context); mImageView.setImageResource(mIconId); int left = 0; int top = 0; int right = 0; int bottom = 0; int orientation = HORIZONTAL; int textViewIndex = 0; switch(mRelation){ case ICON_ABOVE: orientation = VERTICAL; bottom = mSpace; textViewIndex = 1; break; case ICON_BELOW: orientation = VERTICAL; top = mSpace; break; case ICON_LEFT: right = mSpace; textViewIndex = 1; break; case ICON_RIGHT: left = mSpace; break; } this.setOrientation(orientation); this.addView(mImageView); mImageView.setPadding(left, top, right, bottom); this.addView(mTextView, textViewIndex); }
}
0 0
- Andriod: 在xml布局中使用自定义属性
- Andriod: 在xml布局中使用自定义属性
- Andriod: 在xml布局中使用自定义属性
- android 在布局中使用自定义属性
- andriod中自定义属性的使用
- Andriod下完全自定义控件和在自定义控件中使用自定义属性
- Android在xml布局中找不到自定义属性的问题
- 在android style.xml文件中使用自定义属性
- 如何在styles.xml中使用控件自定义属性
- 布局中自定义属性使用模板
- 【Android】 XML布局文件中,使用自定义属性不提示和不生效
- android 如何在XML中自定义属性
- android自定义XML布局属性
- 关于自定义布局,xml中配置属性(attrs)
- Android中自定义布局无法在xml中使用的问题
- 在自定义View中使用自定义属性
- Android Studio中xml使用自定义属性
- Andriod 开发之布局文件XML Imageview的scaleType属性
- GitHub使用总结
- GCC的编译过程
- Java中Set、Map、List的用法与区别
- C++中虚函数的作用
- SVN简介
- Andriod: 在xml布局中使用自定义属性
- 监听屏幕 android.intent.action.USER_PRESENT
- BZOJ 2096
- CSS3实现“圣杯布局”--(固比固布局)
- java线程面试题
- join控制线程的执行顺序
- leetcode(valid-palindrome)判断字符串是否是对称
- Java 教程目录
- CF355div2