【问题标题】:how to set max Width for a Layout如何设置布局的最大宽度
【发布时间】:2012-02-10 23:36:49
【问题描述】:

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

<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>

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

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    查看链接或设置一个值

          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    

    http://developer.android.com/guide/practices/screens_support.html

    【讨论】:

      【解决方案2】:

      您必须创建一个从 LinearLayout 扩展的新 ViewGroup,并将其限制为您想要的宽度。见this answer

      【讨论】:

      • 谢谢,但我正在寻找 XML 解决方案。
      【解决方案3】:

      处理您的情况的适当方法是创建一个单独的布局文件以优化平板电脑的表单视图,同时将现有文件用于手机。为此,您可以创建单独的 res/layout 目录,并附上屏幕尺寸和/或分辨率的限定符。

      您可以阅读有关可用资源限定符here 的更多信息。这里有很多选择,我会让你为你的应用选择最好的,但这里有一个简单的例子:

      假设您当前的布局文件是 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)。这比尝试设置最大宽度更有效,尤其是在设备具有可变分辨率和纵横比时。

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

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

      你可以阅读更多关于TableLayoutin the SDK docs的信息。

      HTH

      【讨论】:

        【解决方案4】:

        按照@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

        【讨论】:

        • 请记住 LinearyLayoutNOTandroid:layout_width="wrap_content" 我犯了这个错误并认为它是必需的,但根本不需要它
        • 不错的解决方案,虽然它的值-w600dp。
        • 由于某种原因,这似乎没有任何效果。视图总是使用 match_parent 而不是更大屏幕上的特定值。
        【解决方案5】:

        这个BoundedViews library 可以帮助你设置 maxWidth/maxHeight 的约束

        【讨论】:

          【解决方案6】:

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

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

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

          【讨论】:

          • 非常感谢它在下班后帮助了我很多 6 小时我找到了这个解决方案......再次感谢
          • 哇!棒极了!很难找到这些信息,这也太不可思议了。
          猜你喜欢
          • 1970-01-01
          • 2018-01-09
          • 2018-09-02
          • 2014-10-30
          • 2016-03-09
          • 1970-01-01
          • 2016-11-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多