What about percentage usage?

放肆的年华 提交于 2021-02-08 03:45:30

问题


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

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

Something like that:

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.

Thanks.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/11782648/what-about-percentage-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!