2013-05-01 44 views
3

如果我定義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)))); 
+0

真的很有趣的問題。 +1 – Blackbelt 2013-05-01 13:28:57

回答

0

我處理這個問題爲好,而最有可能的Android是根據屏幕密度,這在擴大20了原有的價值30你情況下最有可能是240 DPI,這將產生1.5

http://www.androidtablets.net/forum/android-tablet-usage-tips-tricks/9444-lcd-density-explained.html http://developer.android.com/guide/practices/screens_support.html

的縮放因子,可以確認通過打印以下顯示量度這些值 (在Mono/C#中,需要針對原始Android/Java的微小翻譯):

this.ApplicationContext.Resources.DisplayMetrics.Density; this.ApplicationContext.Resources.DisplayMetrics.DensityDpi; this.ApplicationContext.Resources.DisplayMetrics.Xdpi; this.ApplicationContext.Resources.DisplayMetrics.Ydpi; this.ApplicationContext.Resources.DisplayMetrics.WidthPixels; this.ApplicationContext.Resources.DisplayMetrics.HeightPixels;

+0

唯一的解決方法是從維度獲取字符串,而不僅僅是維度本身。此問題僅依賴於textSize,如果您嘗試獲取的值是用sp AOS測量的,將返回縮放值,這與dp測量值不同。 – Stan 2013-05-22 15:10:12

+0

它似乎只與文本大小(好吧,對我來說)。然而,我嘗試了使用「sp」和「dip」度量標準來獲得相同的縮放比例的字體大小,以及幾乎在任何可能的情況下讀取屬性(字符串,引用id,直接屬性,樣式屬性,維度,無dimesn ...)。這裏是一個鏈接到我的具體問題,如果你感興趣http://stackoverflow.com/questions/16679526/android-scaling-density-issues – samosaris 2013-05-22 15:39:13

+0

...哦,等等,我想我得到了雅閱讀字符串...這樣,你得到聲明的值,沒有機會讓android縮放它,那麼你只需要將字符串解析爲一個整數(或其他)。你是這個意思嗎 ? – samosaris 2013-05-22 16:12:36

相關問題