2013-05-01 94 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

相关问题