android 设置布局宽度,android – 如何设置布局的最大宽度

偏执的太偏执、 2022-10-17 11:09 22阅读 0赞

处理您的情况的适当方法是创建一个单独的布局文件,以便在使用现有的手机文件时优化平板电脑的窗体视图。您可以通过创建单独的res / layout目录,其中包含屏幕尺寸和/或分辨率的限定符。

您可以阅读更多关于可用的资源限定词here.有很多选择在这里,我会留给你选择最适合你的应用程序,但这里是一个简单的例子:

我们假设你当前的布局文件是form.xml。

将您现有的布局文件放在res / layout / form.xml中。这将使其成为默认布局。

创建另一个文件并将其放在res / layout-large / form.xml中。此布局文件将用于物理屏幕尺寸> 〜5“(所有标准平板电脑)。为了处理您的问题,我修改了默认布局,以水平方式显示窗体,仅占屏幕宽度的60%

android:id=”@+id/adlayout”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:orientation=”horizontal”

android:gravity=”center_horizontal”

android:weightSum=”1” >

android:layout_width=”0dp”

android:layout_height=”wrap_content”

android:layout_weight=”0.6”

android:stretchColumns=”1” >

android:id=”@+id/textView1”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:padding=”6dip”

android:text=”Barcode Format” >

android:id=”@+id/edit_barcode_format”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:padding=”6dip” >

android:id=”@+id/textView2”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:padding=”6dip”

android:text=”Barcode Type” >

android:id=”@+id/edit_barcode_type”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:padding=”6dip” >

该更改使用LinearLayout的layout_weight和weightSum属性,该属性告诉表仅填充其父级的60%(layout_weight = 0.6)。这比设置最大宽度更有效,特别是当设备具有可变分辨率和宽高比时。

FYI,我还试图删除你在XML中所做的一切不必要的属性,只是创建杂乱(如多个xmlns:android声明)。其中一个更改是从TableLayout孩子中删除额外的layout_参数,因为TableLayout无论如何都忽略所有这些参数,并强制其子项使用某些约束。从文档:

The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined by a child; default value is WRAP_CONTENT. If the child is a TableRow, then the height is always WRAP_CONTENT.

您可以阅读有关TableLayout in the SDK docs的更多信息。

HTH

发表评论

表情:
评论列表 (有 0 条评论,22人围观)

还没有评论,来说两句吧...

相关阅读