Android自定义控件使用declare-styleable进行属性配置
来源:互联网 发布:数据分析是做什么的 编辑:程序博客网 时间:2024/05/21 05:08
1、在res/vlaues文件夹下创建资源文件attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="poster"> <attr name="icon" format="reference" /> <attr name="iconW" format="dimension" /> <attr name="iconH" format="dimension" /> <attr name="text" format="reference|string" /> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="background" format="reference" /> </declare-styleable> <declare-styleable name="settingItem"> <attr name="name" format="reference|string" /> <attr name="value" format="reference|string" /> </declare-styleable></resources>
a. reference:参考某一资源ID,以此类推
(1)属性定义:
<declare-styleable name = "名称"><attr name = "background" format = "reference" /></declare-styleable>
(2)属性使用:
<ImageViewandroid:layout_width = "42dip"android:layout_height = "42dip"android:background = "@drawable/图片ID"/>
b. color:颜色值
<declare-styleable name = "名称"><attr name = "textColor" format = "color" /></declare-styleable>
c. boolean:布尔值
<declare-styleable name = "名称"><attr name = "focusable" format = "boolean" /></declare-styleable>
d. dimension:尺寸值(注意:这里如果是dp那就会做像素转换)
<declare-styleable name = "名称"><attr name = "layout_width" format = "dimension" /></declare-styleable>
e. float:浮点值
f. integer:整型值
g. string:字符串
h. fraction:百分数
i. enum:枚举值
j. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值
k. reference|color:颜色的资源文件
l.reference|boolean:布尔值的资源文件
注意://由于reference是从资源文件中获取,所以在XML文件中写这个属性的时候必须使用personattr:name="@string/app_name"这种格式,否则会出错。
2.设置好属性文件后,在使用的布局中写相关配置
xmlns:myapp="http://schemas.android.com/apk/res/com.starview.tv"
<com.starview.tv.view.PosterView android:id="@+id/myview1" android:layout_width="290dp" android:layout_height="@dimen/settingpage_big_height" android:background="@drawable/hello_selection" myapp:background="@drawable/mainmenu_colorboard_blue" myapp:icon="@drawable/icons_01catbox_dtv" myapp:iconH="110dp" myapp:iconW="110dp" myapp:text="@string/str_dtv" myapp:textColor="@color/white" myapp:textSize="@dimen/indexsecondpagefontsize" />
package com.starview.tv.view;import com.starview.tv.R;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.TextView;public class PosterView extends FrameLayout{ private ImageView mPosterIconImageV; private TextView mPosterTextV; private View mPosterBackground; public PosterView(Context context) { this(context, null); } public PosterView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PosterView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.poster); if (mPosterTextV != null) { String text = a.getString(R.styleable.poster_text); if (text != null) mPosterTextV.setText(text); int color = a.getColor(R.styleable.poster_textColor, Color.WHITE); mPosterTextV.setTextColor(color); float size = a.getDimensionPixelOffset(R.styleable.poster_textSize, 24); mPosterTextV.setTextSize(size); } if (mPosterIconImageV != null) { Drawable d = a.getDrawable(R.styleable.poster_icon); if(d != null) mPosterIconImageV.setImageDrawable(d); int width = a.getDimensionPixelOffset(R.styleable.poster_iconW, 100); int height = a.getDimensionPixelOffset(R.styleable.poster_iconH, 100); ViewGroup.LayoutParams lp = mPosterIconImageV.getLayoutParams(); lp.width = width; lp.height = height; mPosterIconImageV.setLayoutParams(lp); } if(mPosterBackground != null) { Drawable d = a.getDrawable(R.styleable.poster_background); if(d != null) mPosterBackground.setBackgroundDrawable(d); } a.recycle(); } private void initView() { LayoutInflater.from(getContext()).inflate(R.layout.poster_view_layout, this, true); mPosterBackground = findViewById(R.id.poster); mPosterIconImageV = (ImageView) findViewById(R.id.imageView1); mPosterTextV = (TextView) findViewById(R.id.textView1); } public void setImageDrawable(Drawable d) { if(mPosterIconImageV != null) { mPosterIconImageV.setImageDrawable(d); } } public void setImageDrawable(int resId) { if(mPosterIconImageV != null) { mPosterIconImageV.setImageResource(resId); } } public void setText(CharSequence text) { if(mPosterTextV != null) { mPosterTextV.setText(text); } } public void setText(int resId) { if(mPosterTextV != null) { mPosterTextV.setText(resId); } } public ImageView getIconImageView() { return mPosterIconImageV; } public TextView getTextView() { return mPosterTextV; } public void setTextVisibility(int visibility) { if(mPosterTextV != null) { mPosterTextV.setVisibility(visibility); } } public void setIconVisibility(int visibility) { if(mPosterIconImageV != null) { mPosterIconImageV.setVisibility(visibility); } } public void setPosterBackground(Drawable d) { if(mPosterBackground != null) { mPosterBackground.setBackgroundDrawable(d); } } public void setPosterBackground(int resId) { if(mPosterBackground != null) { mPosterBackground.setBackgroundResource(resId); } }}
0 0
- Android自定义控件使用declare-styleable进行属性配置
- Android自定义控件使用declare-styleable进行属性配置
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- 【Android】declare-styleable:自定义控件的属性
- Android declare-styleable自定义控件属性
- Android declare-styleable:自定义控件的属性
- Android declare-styleable自定义控件属性注意事项
- Android declare-styleable自定义控件属性
- android 自定义圆角头像以及使用declare-styleable进行配置属性解析
- 354_出现乱码
- HDU 5916 Harmonic Value Description (2016-ccpc-长春)
- Groovy集合(list)
- 我的一切
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) C(组合数+容斥)
- Android自定义控件使用declare-styleable进行属性配置
- maven install报错: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile
- C++ 内存泄露检测方法
- 355_封装RecyclerView的Adapter
- Nagios 监控 Windows 服务器
- TreeSet 的 compareTo 返回值问题
- codevs1743 反转卡片
- Java终止某个线程的方法
- 19.BOM&DOM-动态操作节点