在我的虚拟 android 设备上,对我来说似乎是这样。
如果不是这种情况,那么我该如何指定,例如。屏幕宽度的 35%,不使用 360*.35 = 126dp?
最佳答案
Is it true that when coding for android 360dp = the whole screen width?
没有。
It seems like that for me, on my virtual android device.
它不是我能想到的任何标准 Android 屏幕分辨率的“整个屏幕宽度”。例如,360dp = 360px 用于中密度屏幕,540px 用于高密度屏幕,我知道恰好有零个 Android 设备在任一维度上都为 360px 或 540px。
另外,请记住,“整个屏幕宽度”会根据设备处于纵向模式还是横向模式而有所不同。
If this isn't the case, then how can I specify eg. 35% of the screen width, without using 360*.35 = 126dp?
使用 LinearLayout
和 android:layout_weight
。 Here is a sample project证明这一点。关键在于布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="Fifty Percent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="50"
/>
<Button
android:text="Thirty Percent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="30"
/>
<Button
android:text="Twenty Percent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="20"
/>
</LinearLayout>
每个小部件都表示没有固有高度,因此总高度根据它们的相对权重在小部件之间分配。
关于android - 为 android 编码时 360dp = 整个屏幕宽度是真的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5445659/