如何处理不同的屏幕尺寸,这样一个TextView秤 [英] How to handle difference screen sizes so that a textview scales

查看:329
本文介绍了如何处理不同的屏幕尺寸,这样一个TextView秤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个均匀分布视图文本。一直无法弄清楚,满足所有的屏幕尺寸甚至是普通屏幕尺寸的方法。我想要实现的布局如下图所示。

I have to distribute text over a view uniformly. Have been unable to figure out a way that satisfies all screen sizes or even the common screen sizes. The layout I want to achieve is shown below.

红色框告诉我努力工作,在TextView的。

The red box tells the textview I am trying to work on.

我曾尝试以下方法:

1)尽量选用形象,并没有9补丁,它不正确缩放。
2)尝试使用不同大小的水桶一样SW320 SW480 sw600和sw720。虽然它确实解决这个问题,因为我能够测试设备上,它不可靠。我把不同的文本大小在这些桶。

1) Try to use a image, with and without nine patch and it does not scale correctly. 2) try to use different size buckets like sw320 sw480 sw600 and sw720. And while it does fix this for devices I am able to test on, it is not dependable. I am putting different text sizes in these buckets.

我讨厌用24 texviews有桌子,网格或线性布局。

I hate to use 24 texviews with table, grid or linear layout.

我有什么其他选择?该解决方案2非常令人沮丧的工作,从梦诗导航布局并再次像20倍左右。应该有这样做更简单的方法,如果心不是请告诉我的。

What other options do I have? The solution 2 was very frustrating to work with, navigating from dimens to layout and back again like about 20 times . There should be an easier way to do this, and if there isnt please tell me so.

推荐答案

使用自定义视图并重写的onDraw得出的数字。包含视图的布局可以使用尺寸(从RES /价值/)指定其大小,什么基于这些维度之间的数字字体大小和间距的视图就会自动工作了。

Use a custom View and override onDraw to draw the numbers. The layout containing the View can specify its size using dimensions (from res/values/), and the View will automatically work out what font size and spacing between the numbers based on those dimensions.

例如

自定义视图:

public class ColumnNumbersView extends View
{
    private final static int NUMBER_OF_COLUMNS = 25;
    private float textSize;
    private final Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ...

获得大小:

    @Override 
    protected void onLayout(...
    {
        super.onLayout(...

        // work out our text size from getHeight()
        textSize = getHeight()/2; // or something like that

        // work out the spacing between the numbers along the x axis
        textPosXInc = (getWidth() / NUMBER_OF_COLUMNS) / 2; // or something like that
    }

在做图:

    @Override
    protected void onDraw(final Canvas canvas)
    {
        super.onDraw(canvas);

        int x = 0;
        for(int i=0; i < NUMBER_OF_COLUMNS; i++)
        {
           final String number = String.valueOf(i);
           final int halfWidth = textPaint.measureText(number) / 2;

           canvas.drawText(number, x - halfWidth, 0, textPaint);

           x += textPosXInc; 
        }
    }

这应引起密切的东西,第一个和最后一个号码将无法正确绘制,虽然,我会留给你们自己来解决。

That should draw something close, the first and last numbers won't draw correctly though, I'll leave that for you to fix.

修改

请记住,你不能使用WRAP_CONTENT对这一观点的尺寸,因为它没有指定大小通过覆盖onMeasure包()。所以它会需要特定的尺寸设置,或match_parent。

Keep in mind that you can't use wrap_content for this View's dimensions, because it doesn't specify a size to wrap by overriding onMeasure(). So it'll require specific sizes set, or match_parent.

这篇关于如何处理不同的屏幕尺寸,这样一个TextView秤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆