我正在尝试为我的按钮创建自定义属性,但我不知道在属性声明中我必须使用哪种格式的图像...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TCButton">
<attr name="Text" format="string"/>
<attr name="BackgroundImage" format="android:drawable" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
错误是格式="android:drawable"...
JOG*_*JOG 152
您可以使用format ="integer",drawable 的资源ID和AttributeSet.getDrawable(...).
这是一个例子.
在res/values/attrs.xml中将该属性声明为整数:
<resources>
<declare-styleable name="MyLayout">
<attr name="icon" format="integer" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
将属性设置为布局中的可绘制ID:
<se.jog.MyLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
myapp:icon="@drawable/myImage"
/>
Run Code Online (Sandbox Code Playgroud)
从自定义窗口小部件组件类中的属性获取drawable:
ImageView myIcon;
//...
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyLayout);
Drawable drawable = a.getDrawable(R.styleable.MyLayout_icon);
if (drawable != null)
myIcon.setBackgroundDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)
要查看所有可能的选项,请在此处查看android src
Ion*_*gru 35
我认为最好将它作为一个简单的参考:
<declare-styleable name="TCButton">
<attr name="customText" format="string"/>
<attr name="backgroundImage" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
并在你的xml中设置如下:
<your.package.name.TCButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:customText="Some custom text"
custom:backgroundImage="@drawable/myImage"
/>
Run Code Online (Sandbox Code Playgroud)
在你的班级中设置如下属性:
public TCButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MembershipItemView, 0, 0);
String customText;
Drawable backgroundImage;
try {
customText = a.getString(R.styleable.TCButton_customText);
backgroundImage = a.getDrawable(R.styleable.TCButton_backgroundImage);
} finally {
a.recycle();
}
if(!TextUtils.isEmpty(customText)) {
((TextView)findViewById(R.id.yourTextView)).setText(customText);
}
if(null != backgroundImage) {
((ImageView)findViewById(R.id.yourImageView)).setBackgroundDrawable(backgroundImage);
}
}
Run Code Online (Sandbox Code Playgroud)
PS:不要忘记为您使用自定义视图的布局的根元素添加此行
xmlns:custom="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)
如果未设置此选项,则无法访问自定义属性.
CoX*_*ier 10
从AOSP代码中,我发现了Google工程师如何声明ImageView#srcattr。
<declare-styleable name="ImageView">
<attr name="src" format="reference|color" />
<attr name="scaleType">
<enum name="matrix" value="0" />
<enum name="fitXY" value="1" />
<enum name="fitStart" value="2" />
<enum name="fitCenter" value="3" />
<enum name="fitEnd" value="4" />
<enum name="center" value="5" />
<enum name="centerCrop" value="6" />
<enum name="centerInside" value="7" />
</attr>
<attr name="adjustViewBounds" format="boolean" />
<attr name="maxWidth" format="dimension" />
<attr name="maxHeight" format="dimension" />
<attr name="tint" format="color" />
<attr name="baselineAlignBottom" format="boolean" />
<attr name="cropToPadding" format="boolean" />
<attr name="baseline" format="dimension" />
<attr name="drawableAlpha" format="integer" />
<attr name="tintMode" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
上面的代码是一个示例,它可以涵盖我们开发中的大多数情况。
| 归档时间: |
|
| 查看次数: |
43842 次 |
| 最近记录: |