如何为布局设置最大宽度

28

经过大量的谷歌搜索后,我无法找到解决这个问题的方法。我有这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/adlayout"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
<TableLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:stretchColumns="1"
   android:layout_weight="1"
  >

  <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent"   android:layout_height="wrap_content">
       <TextView android:padding="6dip" android:text="@string/barcode_format"  android:id="@+id/textView1" android:layout_width="wrap_content"   android:layout_height="wrap_content"></TextView>
       <EditText android:padding="6dip" android:layout_width="fill_parent" android:id="@+id/edit_barcode_format" android:layout_height="wrap_content"></EditText>
  </TableRow>
  <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content">
      <TextView android:padding="6dip" android:text="@string/barcode_type" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
      <EditText android:padding="6dip" android:layout_width="fill_parent" android:id="@+id/edit_barcode_type" android:layout_height="wrap_content"></EditText>
  </TableRow>        
 </TableLayout>


</LinearLayout>
布局定义了一个表单。在手机上看起来还好,但在平板电脑上,编辑文本视图显得太大了。我想为布局设置最大宽度,并将表单居中绘制。
6个回答

49

按照@Devunwired所建议的,保留不同的布局文件是正确的,但是这样会增加项目的复杂性(如果设计师重新设计视图布局,则必须维护多个文件)。

如果你只需要修改一个按钮的宽度,我建议使用以下方法。在layout文件夹中保留一个xml文件,并给它一个值

<LinearLayout
    style="@style/width_match_parent_max_200"
    android:layout_height="wrap_content">

values-w600dp/styles.xml 包含的内容

<resources>
    <style name="width_match_parent_max_200">
        <item name="android:layout_width">200dp</item>
    </style>
</resources>

values/styles.xml 包含

<resources>
    <style name="width_match_parent_max_200">
        <item name="android:layout_width">match_parent</item>
    </style>
</resources>

编辑:请注意,文件夹名称为values-w600dp而不是values-600dp。


请记住,LinearyLayout 不会有 android:layout_width="wrap_content"。我犯了这个错误,认为它是必需的,但实际上根本不需要。 - Christopher Rucinski
不错的解决方案,尽管它的值为w600dp。 - ade.akinyede
由于某些原因,这似乎没有任何效果。在更大的屏幕上,视图始终使用match_parent而不是特定的值。 - RamPrasadBismil

29
请使用 ConstraintLayout。在其中,您可以使用以下属性来设置最小/最大宽度/高度:
  • app:layout_constraintWidth_min
  • app:layout_constraintWidth_max
  • app:layout_constraintHeight_min
  • app:layout_constraintHeight_max
不要忘记将您的视图的宽度/高度设置为与约束相匹配(0dp)。

1
非常感谢,这对我帮助很大。在工作了6个小时后,我只找到了这个解决方案...再次感谢。 - Rahul Singh Rawat
1
哇!太棒了!这也令人难以置信,找到这些信息竟然如此困难。 - Machado

16

处理你的情况的正确方法是创建一个单独的布局文件,以优化平板电脑上的表单视图,同时在手机上使用现有文件。您可以通过创建带有屏幕大小和/或分辨率限定符的单独的res/layout目录来实现此目的。

您可以在这里阅读有关可用资源限定符的更多信息。这里有许多选择,我会让您自己选择最适合您的应用程序的选项,但是这是一个简单的示例:

假设您当前的布局文件是 form.xml

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

创建另一个文件并将其放在 res/layout-large/form.xml 中。该布局文件将在物理屏幕大小>〜5英寸(所有标准平板电脑)的设备上使用。为了解决您的问题,我已修改了默认布局,使其水平居中显示表单,并仅占用屏幕宽度的60%:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:weightSum="1" >

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:stretchColumns="1" >

        <TableRow android:id="@+id/tableRow1">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="6dip"
                android:text="Barcode Format" >
            </TextView>

            <EditText
                android:id="@+id/edit_barcode_format"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="6dip" >
            </EditText>
        </TableRow>

        <TableRow android:id="@+id/tableRow1">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="6dip"
                android:text="Barcode Type" >
            </TextView>

            <EditText
                android:id="@+id/edit_barcode_type"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="6dip" >
            </EditText>
        </TableRow>
    </TableLayout>

</LinearLayout>

该更改利用了LinearLayoutlayout_weightweightSum属性,这告诉表格只填充其父类的60%(layout_weight=0.6)。当设备具有可变分辨率和纵横比时,这比尝试设置最大宽度更有效。

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

TableLayout的子项不能指定layout_width属性。 宽度始终为MATCH_PARENT。 但是,子项可以定义layout_height属性;默认值为WRAP_CONTENT。 如果子项是TableRow,则高度始终为WRAP_CONTENT。

你可以在SDK文档中了解更多关于TableLayout的信息。

希望对你有帮助。


2

您需要创建一个新的ViewGroup,它继承自LinearLayout,并通过指定宽度来限制其大小。请参见这个答案


1
谢谢,但我正在寻找一个XML解决方案。 - MaikoMobile

0

这个BoundedViews库可以帮助您设置maxWidth/maxHeight的约束条件。


-22

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接