【问题标题】:Setting textSize in custom view results in huge text在自定义视图中设置 textSize 会产生巨大的文本
【发布时间】:2014-12-15 20:27:39
【问题描述】:

我在自定义视图的构造函数中调用以下代码:

private void style(Resources.Theme theme, AttributeSet attrs) {
    TypedArray a = theme.obtainStyledAttributes(
            attrs,
            R.styleable.StackedTextView,
            0, 0);

    try {
        DebugTool.assertTrue(holdr != null, "View holder has not been properly intialized.");
        String line1 = a.getString(R.styleable.StackedTextView_line1);
        setLine1(line1);
        String line2 = a.getString(R.styleable.StackedTextView_line2);
        setLine2(line2);

        line1Size = a.getDimension(R.styleable.StackedTextView_line1_textSize, 20);
        line2Size = a.getDimension(R.styleable.StackedTextView_line2_textSize, 20);
        if (line1Size > 0) {
            holdr.textLine1.setTextSize(line1Size);
        }
        if (line2Size > 0) {
            holdr.textLine2.setTextSize(line2Size);
        }

    } finally {
        a.recycle();
    }
}

它应该为2个文本字段设置文本和文本大小。

除了文本内容的字符串格式(工作正常)之外,我的 attr.xml 中还有以下内容。

    <attr name="line1_textSize" format="dimension" />
    <attr name="line2_textSize" format="dimension" />

当我使用此视图并通过 xml 使用尺寸设置文本大小时,

        <com.me.app.view.component.StackedTextView
            android:id="@+id/overview_total_reviews"
            app:line1="40"
            app:line2="Rating"
            style="@style/OverviewStackedText"
            app:line1_textSize="10sp"
            app:line2_textSize="12sp"
            />

文本最终比预期的要大得多。我只分别设置了 10 和 12sp,文字大小更接近 30sp。

谁能看到我做错了什么?我是否需要对 DisplayMetrics 进行一些操作以确保正确缩放?

编辑:添加一些说明

维度正在被拾取。当我使用自定义属性(在 xml 中)设置不同的文本大小时,文本确实会发生变化。我也试过使用getDimensionPixelSize

好像计算/维度检索是错误的。 1sp(或 dp)变化会导致显着变化。

【问题讨论】:

    标签: android android-layout android-custom-view


    【解决方案1】:

    请尝试以下方法:

    line1Size = a.getDimensionPixelSize(R.styleable.StackedTextView_line1_textSize, 0);
    line2Size = a.getDimensionPixelSize(R.styleable.StackedTextView_line2_textSize, 0);
    
    if (line1Size > 0) {
        holdr.textLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, line1Size);
    }
    if (line2Size > 0) {
        holdr.textLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, line2Size);
    }
    

    【讨论】:

    • 要为这个正确的解决方案添加一些说明,我需要首先检索以像素为单位的尺寸,然后通过传递我在第二个参数中传递的尺寸的单位/类型来设置文本大小。是的。
    • 我的错。默认setTextSize() 使用COMPLEX_UNIT_SP 并导致此错误。
    【解决方案2】:

    @loeschg 我遇到了同样的问题,但我终于设法解决了使用@plackemacher 的回复而不是link

    方法.getDimensionPixelSizealways()返回像素值,所以你必须使用convertPixelsToDp方法来获取dp值。

    下面是可以帮助你的代码:

     /**
     * This method converts device specific pixels to density independent pixels.
     * 
     * @param px A value in px (pixels) unit. Which we need to convert into db
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent dp equivalent to px value
     */
    public static float convertPixelsToDp(float px, Context context){
        return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    }
    
    line1Size = convertPixelsToDp(a.getDimensionPixelSize(R.styleable.StackedTextView_line1_textSize, 0), some_context);
    line2Size = convertPixelsToDp(a.getDimensionPixelSize(R.styleable.StackedTextView_line2_textSize, 0), some_context);
    
    if (line1Size > 0) {
        holdr.textLine1.setTextSize(TypedValue.COMPLEX_UNIT_PX, line1Size);
    }
    if (line2Size > 0) {
        holdr.textLine2.setTextSize(TypedValue.COMPLEX_UNIT_PX, line2Size);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      相关资源
      最近更新 更多