我创建了一个简单的自定义视图,其中包含a RelativeLayout和EditText:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
此外,我添加了一些自定义属性res/values/attrs.xml,我在自定义视图构造函数中检索这些属性,一切正常.
现在,我想EditText在自定义视图中检索默认属性,例如我想android:text在自定义视图中获取属性.
我的自定义视图类(简化):
public class CustomEditText extends RelativeLayout {
private int panCount;
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomEditText, 0, 0);
try {
this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16);
} finally {
typedArray.recycle();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果不重新声明文本属性,我怎么能这样做res/values/attrs.xml呢?
android android-widget custom-view android-custom-view android-attributes
我的Drawable-mdpi文件夹中有一个名为(my_image.png)的图像.
我的android应用程序与webservice交互.它可能会发回"my_image".我的Android应用程序应该加载此图像.
我正在使用Monodroid,我试过这个
int abc = Resources.GetIdentifier("my_image","drawable",null);
Run Code Online (Sandbox Code Playgroud)
但结果总是如此"0".什么时候应该(从资源文件)
// aapt resource value: 0x7f020000
public const int my_image = 2130837504;
Run Code Online (Sandbox Code Playgroud)
环顾四周,android方式似乎相似
int i = this.getResources().getIdentifier("txt_asecondtext", "strings", this.getPackageName());
Run Code Online (Sandbox Code Playgroud)
我尝试传递包名称而不是null但没有做任何事情.