自定义属性
2015-12-21 17:26
120 查看
参照:
sdk->platforms->android-17->data->res->values->attrs.xml
思路:
在values下建一个attrs.xml
在布局中自定义一个命名空间
在自定义view中获取属性值
code如下
传承者(Inheritors)欢迎各位纠正错误,评论,吐槽!!!
sdk->platforms->android-17->data->res->values->attrs.xml
思路:
在values下建一个attrs.xml
在布局中自定义一个命名空间
在自定义view中获取属性值
code如下
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="HomeItemContentView"> <attr name="home_title" format="string" /> </declare-styleable> </resources>
自定义的view
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; /** * Created by admin on 2015/12/19. */ public class HomeItemContentView extends RelativeLayout { private ImageView mHomeIv;//主页内容图片 private TextView mHomeTxt;//主页内容标题 private String title; private static final String NAMESPASE = "http://schemas.android.com/apk/res-auto";//命名空间 public HomeItemContentView(Context context) { this(context,null); } public HomeItemContentView(Context context, AttributeSet attrs) { this(context, attrs,0); } public HomeItemContentView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); title = attrs.getAttributeValue(NAMESPASE, "home_title"); //根据属性名获得属性值 } /** * 初始化布局 */ private void initView() { //将自定义好的布局文件设置给当前的SettingItemView View.inflate(getContext(), R.layout.home_content_part, this); mHomeIv = (ImageView) findViewById(R.id.iv_attendance); mHomeTxt = (TextView) findViewById(R.id.txt_title); setHomeContentTitle(title);//设置主页标题 } /** * 设置图片 * @param ImageId */ public void setHomeContentImage(int ImageId){ mHomeIv.setImageResource(ImageId); } /** * 设置标题 * @param title */ public void setHomeContentTitle(String title){ mHomeTxt.setText(title); } }
属性添加
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <!--自己命名空间--> xmlns:Inheritors="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/baseColorGrey" android:orientation="vertical" tools:context="com.energy.mytest.MainActivity"> <com.energy.mytest.HomeItemContentView android:id="@+id/home_attendance" android:layout_width="match_parent" android:layout_height="wrap_content" <!--自己定义的属性--> Inheritors:home_title="考勤" /> </LinearLayout>
自定义布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lin_home_content_part" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" > <ImageView android:id="@+id/iv_attendance" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:src="@mipmap/home_attendance" android:layout_centerVertical="true" android:paddingRight="10dp"/> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_toRightOf="@id/iv_attendance" android:textSize="16sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_below="@id/iv_attendance" android:layout_toRightOf="@id/iv_attendance" android:background="#d2d2d2" /> </RelativeLayout>
传承者(Inheritors)欢迎各位纠正错误,评论,吐槽!!!
相关文章推荐
- 一些有用的主页
- Visual C# Linq 找不到 SqlFuntions 解决方案
- Spring中的ContextLoaderListener使用
- sleep(0)
- javaweb开发-js代码中的function.call的参数
- 「Unity」使用TexturePacker的插件TexturePackerImporter.dll 编译报错解决方案
- [.NET源码] GMap扩展
- 7年来都做了些什么
- 20个命令行工具监控 Linux 系统性能
- spring事务隔离级别
- CSS3-学习记录一
- 优化表的数据类型
- 老李分享:jvm内存原型剖析
- 身份证校验js,包括了第18位数字的校验
- html获取屏幕宽度及其分辨率
- hive 常用查询语句
- 类型的安全转换
- iOS常见的几种加密和比较
- HTTP Header 属性列表
- Navigation 动态加载选项,HorizontalScrollView横向选项卡