android - AttributeSet 值返回 @2131296269 - 它是什么以及如何使用?

标签 android xml

如果我在 xml 中定义一些 View ,例如:

        <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

更新
我进行了一项研究,发现 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))));

最佳答案

我也在处理这个问题,很可能 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;

关于android - AttributeSet 值返回 @2131296269 - 它是什么以及如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318594/

相关文章:

android - 如何在android中打开系统数据使用 Activity ?

android - 与 viewpager 和 recyclerview 一起使用时,CollapsingToolbarLayout 的部分内容会粘在顶部

android - 平板电脑上没有统一编码的超棒字体图标(ios 和 android)

python - PySide 中 Qt.escape 的替代品?

android - M 新保护级别列表?

android - 将 Admob 代码添加到 Android 给出错误 : You must have AdActivity declared in AndroidManifest. xml

c++ - XercesDOMParser 和 XIncludes

C# [XmlElement] 属性不隐式转换类型?

java - 是否可以在代码中设置单个 View 组的内容 View ?

java - Android GridView 文本重叠