如果我定義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