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

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

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

最佳答案

按照@Devunwired 的建议保留不同的布局文件是正确的,但是,它会增加项目的复杂性(您必须维护多个文件,以防设计师重新设计 View 布局)。

如果您只需要更改按钮的宽度,我建议您执行以下操作。在 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

关于android - 如何设置布局的最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873059/

相关文章:

Android 自定义 map 标记、 Canvas 绘图和图像大小调整

android - RecyclerView 中特定 TextView 的滚动条不起作用

android - 从 Activity 获取 ListView 并以编程方式填充 XML 字符串数组

android - 如何以编程方式将图像添加到 LinearLayout?

android - 如何使用另一个布局的新实例来扩充布局?

android - 在 Android 深层链接的特定 url 处提供 json 文件

android - 从服务启动 AlertDialog.Builder

Android:Fragment !isDetached() 和 isAdded() 有什么区别?

android - 将 RelativeLayout 置于 RelativeLayout 下方

android - 如何在线性布局中将元素放置得更近?