如何在我的自定义视图中使用标准属性 android:text?
- 作者: 香气CG
- 来源: 51数据库
- 2022-12-30
问题描述
我编写了一个扩展RelativeLayout 的自定义视图.我的视图有文本,所以我想使用标准 android:text 而不 需要指定 <declare-styleable> 和 <每次我使用自定义视图时,都没有使用自定义命名空间 xmlns:xxx.
I wrote a custom view that extends RelativeLayout. My view has text, so I want to use the standard android:text without the need to specify a <declare-styleable> and without using a custom namespace xmlns:xxx every time I use my custom view.
这是我使用自定义视图的 xml:
this is the xml where I use my custom view:
<my.app.StatusBar
android:id="@+id/statusBar"
android:text="this is the title"/>
如何获取属性值?我想我可以使用
How can I get the attribute value? I think I can get the android:text attribute with
TypedArray a = context.obtainStyledAttributes(attrs, ???);
但是在这种情况下 ??? 是什么(在 attr.xml 中没有样式)?
but what is ??? in this case (without a styleable in attr.xml)?
推荐答案
使用这个:
public YourView(Context context, AttributeSet attrs) {
super(context, attrs);
int[] set = {
android.R.attr.background, // idx 0
android.R.attr.text // idx 1
};
TypedArray a = context.obtainStyledAttributes(attrs, set);
Drawable d = a.getDrawable(0);
CharSequence t = a.getText(1);
Log.d(TAG, "attrs " + d + " " + t);
a.recycle();
}
希望你有想法
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0
