如何设置布局的最大宽度

Mai*_*ile 23 android android-layout android-linearlayout

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

<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>
Run Code Online (Sandbox Code Playgroud)

表格布局定义了一个表格.在手机上看起来不错,但在平板电脑上,edittext视图看起来太大了.我想为布局设置最大宽度,并将窗体绘制为居中.

kou*_*nho 46

保持不同的布局文件为@Devunwired建议是正确的,但是,它增加了项目的复杂性(如果设计师重新设计视图布局,则必须维护多个文件).

如果您只需要改变按钮的宽度,我建议如下.将一个xml文件保留在布局文件夹中,并为其指定值

<LinearLayout
    style="@style/width_match_parent_max_200"
    android:layout_height="wrap_content">
Run Code Online (Sandbox Code Playgroud)

values-w600dp/styles.xml包含

<resources>
    <style name="width_match_parent_max_200">
        <item name="android:layout_width">200dp</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

values/styles.xml包含

<resources>
    <style name="width_match_parent_max_200">
        <item name="android:layout_width">match_parent</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

编辑:注意文件夹是值-w600dp而不是值-600dp


Cri*_*tan 16

使用约束布局。在其中,您可以将这些属性用于最小/最大宽度/高度:

  • app:layout_constraintWidth_min
  • app:layout_constraintWidth_max
  • app:layout_constraintHeight_min
  • app:layout_constraintHeight_max

不要忘记设置视图的宽度/高度以匹配约束 ( 0dp)。


Dev*_*red 14

处理您的情况的适当方法是创建一个单独的布局文件,以优化平板电脑的表单视图,同时使用现有的手机文件.您可以通过创建单独的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>
Run Code Online (Sandbox Code Playgroud)

更改使用layout_weightweightSum属性LinearLayout,告诉表只填充layout_weight=0.6其父级的60%().这比尝试设置最大宽度更有效,尤其是当设备具有可变分辨率和宽高比时.

仅供参考,我还尝试删除您在XML中除了创建混乱(例如多个xmlns:android声明)之外什么也没做的任何不必要的属性.其中一个变化是layout_TableLayoutTableLayout节点中删除额外的参数,因为无论如何忽略所有这些参数并强制其子节点使用某些约束.来自Docs:

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

您可以TableLayout 在SDK文档中阅读更多相关信息.

HTH