我正在构建一个应用程序,它以类似于普通 PIN 输入屏幕的 PIN 屏幕开始,问题是我无法让设计在所有类型的手机上运行,为了简单起见,我们只讨论纵向模式(我将链接下面的布局 xml):
- 在小屏幕设备上,我需要减小按钮的 dp 尺寸,这样它们就不会卡在屏幕边缘
- 在大型设备上,按钮更大,因此它们不会在屏幕中间聚集在一起
- 我需要支持一些旧的 (2.3) 设备,其中之一是 5 英寸左右的平板电脑,按钮悬在其上(因为尺寸因素属于大类别),但它应该属于正常尺寸类别。我知道最小宽度尺寸组,但这仅适用于 >3.2 设备。
我希望所有设备上的外观都保持一致,(很大程度上)与其形式或大小无关。我考虑过元素的动态调整大小,但不支持它,而且我不想接近 AbsoluteLayout 类。
有人分享我的挫败感,或者已经开发出好的解决方案吗?
我正在使用的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" >
<TextView android:id="@+id/enter_pin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/DIALOG_ENTERPIN" />
<EditText android:id="@+id/pin_box"
android:layout_below="@id/enter_pin"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_below="@id/pin_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow android:gravity="center" >
<Button android:id="@+id/pin_1"
style="@style/pinbutton"
android:text="1" />
<Button android:id="@+id/pin_2"
style="@style/pinbutton"
android:text="2" />
<Button android:id="@+id/pin_3"
style="@style/pinbutton"
android:text="3" />
</TableRow>
<TableRow android:gravity="center" >
<Button android:id="@+id/pin_4"
style="@style/pinbutton"
android:text="4" />
<Button android:id="@+id/pin_5"
style="@style/pinbutton"
android:text="5" />
<Button android:id="@+id/pin_6"
style="@style/pinbutton"
android:text="6" />
</TableRow>
<TableRow android:gravity="center" >
<Button android:id="@+id/pin_7"
style="@style/pinbutton"
android:text="7" />
<Button android:id="@+id/pin_8"
style="@style/pinbutton"
android:text="8" />
<Button android:id="@+id/pin_9"
style="@style/pinbutton"
android:text="9" />
</TableRow>
<TableRow android:gravity="center" >
<Button android:id="@+id/pin_back"
style="@style/pinbutton"
android:text="‹" />
<Button android:id="@+id/pin_0"
style="@style/pinbutton"
android:text="0" />
<Button android:id="@+id/pin_ok"
style="@style/pinbutton"
android:text="OK" />
</TableRow>
</TableLayout>
</RelativeLayout>
更新:
我已经针对不同的尺寸组使用了不同的布局,并且专门使用 dp 和 sp,仅供引用,所以请不要写泛泛之谈。问题仍然在于细节,在我看来,尺寸箱的选择并不好。
最佳答案
I thought about dynamic sizing of elements, but there is no support for it
这是什么意思?动态调整大小或多或少是 Android 布局的基础。您可以使用 DP,正如您提到的基于密度的相对大小,您可以将 layout_weight
与 LinearLayouts 一起使用来处理显示的小数部分,您可以使用 wrap_content
和 match_parent
使图形大小基于显示器的大小或里面的内容。我的建议首先是为按钮提供 min_width
/min_height
属性,这样在较大的设备上按钮就不会变得太大而荒谬,但也不要太小而无法按在较小的设备上。
如果您不介意布局文件有几个不同版本,则另一种方法是拥有更多符合资源要求的布局文件夹(例如 layout-large
、layout-小
等),布局略有不同,具体取决于显示器的一般尺寸。
编辑:好的,快速的想法,只是对于密码键盘,我什至没有在布局编辑器中查看它,但这是我可能如何做到这一点的基本想法。 LinearLayout
没有 maxWidth 或 maxHeight 属性,因此我可能会为 layout_sw600dp
和 layout_sw720dp
创建一个资源文件夹,并为适用于该尺寸屏幕的密码键盘。
<LinearLayout
android:id="@+id/pin_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max_width="480dp"
android:max_height
android:layout_margin="20dp"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pin_row1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/num1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="1"
/>
<Button
android:id="@+id/num2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="2"
/>
<Button
android:id="@+id/num3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="3"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/pin_row2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/num4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="4"
/>
<Button
android:id="@+id/num5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="5"
/>
<Button
android:id="@+id/num6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="6"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/pin_row3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/num7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="7"
/>
<Button
android:id="@+id/num8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="8"
/>
<Button
android:id="@+id/num9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="9"
/>
</LinearLayout>
</LinearLayout>
关于android-layout - 针对多种尺寸因素的 Android UI 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12319692/