百分比使用率如何? [英] What about percentage usage?

查看:80
本文介绍了百分比使用率如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在考虑百分比形式的用法,例如,在边距中.

So, i'm thinking about the usage of percent, example, in margins.

我们可以使用叠加层按dpi大小分隔布局,但是为什么不能使用百分比呢?

We can use overlay to separate our layout by dpi sizes, but why can't we use percentage?

类似的东西:

Display display = getWindowManager().getDefaultDisplay();

float width = display.getWidth();
float height = display.getHeight();

//20% of left margin
float imageMarginLeft = ( width * 20 ) / 100;

然后将此边距设置为图像或任何元素.

And then set this margin to the image, or whatever element.

这不好吗?我只是想讨论一下.

Is this bad? I just wanna discuss about it.

谢谢.

推荐答案

LinearLayout可以通过使用权重来执行百分比.

LinearLayout can do percentages by using weight.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="100">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:text="Button" />
</LinearLayout>

如果大量使用基于布局的布局,则会影响性能,尤其是在嵌套的情况下.以编程方式执行此操作也可能会影响布局时间.

Weight based layouts hinder performance if you use a lot of them, especially so when they're nested. Doing it programmatically will likely impact layout time as well.

编辑由于您正在寻求讨论,因此我建议基于百分比的布局通常是不好的.问题在于,实际的屏幕尺寸在Android上差异很大.令人难以置信的屏幕的一半与Galaxy S III的屏幕有很大的不同,这与Nexus 7的屏幕有很大的不同,依此类推.使用dp设备的好处在于它们与实际尺寸有关屏幕上的东西.在小屏幕上,按钮不会太小(很难选择),在大屏幕上,按钮不会太大(很丑).

Edit Since you're looking for discussion, I would suggest that percentage based layouts are bad in general. The problem is that physical screen size varies so much with Android. Half the screen on an Incredible is much different than half the screen on a Galaxy S III, which is much different from half the screen on a Nexus 7, etc. The benefit of using dp units is that they're related to the physical size of things on the screen. It keeps buttons from being tiny (and hard to select) on tiny screens and it keeps them from being enormous (and ugly) on large screens.

这篇关于百分比使用率如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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