gpt4 book ai didi

android - 使用百分比呢?

转载 作者:行者123 更新时间:2023-11-29 00:37:18 25 4
gpt4 key购买 nike

所以,我在考虑百分比的用法,例如,在边距中。

我们可以使用 overlay 按 dpi 大小分隔我们的布局,但为什么我们不能使用百分比?

类似的东西:

Display display = getWindowManager().getDefaultDisplay();

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

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

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

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

谢谢。

最佳答案

LinearLayout 可以通过权重来做百分比。

<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>

如果您使用大量基于权重的布局会影响性能,尤其是当它们嵌套时。以编程方式执行此操作也可能会影响布局时间。

编辑既然你在寻找讨论,我建议基于百分比的布局通常是不好的。问题在于物理屏幕尺寸因 Android 而异。 Incredible 的一半屏幕与 Galaxy S III 的一半屏幕有很大不同,后者与 Nexus 7 等的一半屏幕有很大不同。使用 dp 单位的好处是它们与物理尺寸相关屏幕上的东西。它可以防止按钮在小屏幕上变小(难以选择),也可以防止它们在大屏幕上变大(变丑)。

关于android - 使用百分比呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com