如果我定义XML像一些观点:AttributeSet值返回@ 2131296269 - 它是什么以及如何使用?
<com.android.view.AlphabetButton
android:id="@+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:texSize="@dimen/alphabet_button_text_size"/>
,然后在初始化用的AttributeSet:
public AlphabetButton(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs!=null){
String textSizeAttribute = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
这textSizeAttribute
值是这样的:
05-01 16:00 :21.154:I/AlphabetButton(8738):textSizeAttribute @ 2131296269
我认为这就像通过R链接到需要的属性。问题是如何评估它?我试过了:
if (textSizeAttribute.substring(0, 1).equals("@"))
Log.i(this, "textSize is "+context.getResources().getDimension(Integer.valueOf(textSizeAttribute.substring(1))));
但是这会返回错误的值。如果我将其定义为android:texSize="20sp"
而不是@ dimen-link,则textSizeAttribute
返回20.0sp
,这是正确的。
UPDATE
我做了一个调查,发现2131296269(0x7f09000d)链接到我的R.java修正值:
public static final class dimen {
public static final int alphabet_button_width=0x7f09000b;
public static final int alphabet_button_height=0x7f09000c;
public static final int alphabet_button_text_size=0x7f09000d;
...
和dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="alphabet_button_width">30dp</dimen>
<dimen name="alphabet_button_height">30dp</dimen>
<dimen name="alphabet_button_text_size">20sp</dimen>
...
但它返回30而不是定义20不能为什么......这里是日志输出:
个05-01 16:18:14.284: I/AlphabetButton(9418): textSizeAttribute @2131296269
05-01 16:18:14.284: I/AlphabetButton(9418): R.id is 2131296269
05-01 16:18:14.284: I/AlphabetButton(9418): textSize is 30.0
和相应的代码行:
public AlphabetButton(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs!=null){
String textSizeAttribute = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
Log.i(this, "textSizeAttribute "+textSizeAttribute);
Log.i(this, "R.id is "+textSizeAttribute.substring(1));
Log.i(this, "textSize is "+context.getResources().getDimension(Integer.valueOf(textSizeAttribute.substring(1))));
真的很有趣的问题。 +1 – Blackbelt 2013-05-01 13:28:57