【问题标题】:Why EditText is not expanding to fill_parent为什么 EditText 没有扩展到 fill_parent
【发布时间】:2012-07-21 04:05:38
【问题描述】:

我编写了一个 XML 代码,我希望在其中扩展 EditText 以适合父宽度。花了这么多时间,我找不到扩展EditText的方法。

这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TableRow 
        android:layout_marginLeft="10dp" 
        android:layout_marginBottom="15dip">        
        <TextView
            android:text="Comment"
            android:layout_width="match_parent"
            android:textStyle="bold"
            android:padding="3dip" />      
    </TableRow>

    <TableRow 
        android:layout_marginLeft="10dp" 
        android:layout_marginBottom="15dip">        
       <EditText 
            android:id="@+id/EditText02"
            android:layout_height="wrap_content"
            android:lines="5" 
            android:gravity="top|left" 
            android:inputType="textMultiLine"
            android:scrollHorizontally="false" 
            android:minWidth="10.0dip"
            android:maxWidth="5.0dip"/>
    </TableRow>

    <TableRow 
        android:layout_marginLeft="10dp" 
        android:layout_marginBottom="15dip"
        android:gravity="center">           
       <Button 
            android:id="@+id/cancel"
            android:text="Next" />       
   </TableRow>
</TableLayout>
</LinearLayout>

我在这里做错了什么。请帮帮我。

【问题讨论】:

    标签: android android-edittext fill-parent


    【解决方案1】:

    不确定是否严重。

    您将 EditText 的最小宽度 (minWidth) 设置为大于其最大宽度 (maxWidth),并且您没有给它一个 layout_width 属性(您应该将其设置为 match_parent)。

    【讨论】:

    • 我已经给它一个 layout_width 来匹配_parent,但没有任何改变。
    • 同时删除 maxWidthminWidth 属性。
    【解决方案2】:

    我认为您缺少 LinearLayout 中的 android:orientation 属性

    其次,如果这就是您所需要的,您可以避免使用TableLayout,而只使用一个LinearLayout,其中包含android:orientation="vertical" 和所有其他三个View

    【讨论】:

    【解决方案3】:

    在每一行中添加一个额外的 TextView 列。

    此代码可能会对您有所帮助。

    <?xml version="1.0" encoding="UTF-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    
    <TableLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:stretchColumns="1"
        android:orientation="vertical">
    
        <TableRow 
            android:layout_width="fill_parent"
            android:layout_marginLeft="10dp" 
            android:layout_marginBottom="15dip"> 
    
            <TextView android:text="" />    
    
            <TextView
                android:text="Comment"
                android:layout_width="match_parent"
                android:layout_marginRight="10dp"
                android:textStyle="bold"
                android:padding="3dip" />      
        </TableRow>
    
        <TableRow 
            android:layout_marginLeft="10dp" 
            android:layout_marginBottom="15dip"> 
    
            <TextView android:text="" />        
    
           <EditText 
                android:id="@+id/EditText02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:lines="5" 
                android:gravity="top|left" 
                android:inputType="textMultiLine"
                android:scrollHorizontally="false" />
        </TableRow>
    
        <TableRow 
            android:layout_marginLeft="10dp" 
            android:layout_marginBottom="15dip"
            android:gravity="center">
    
            <TextView android:text="" />        
    
           <Button 
                android:id="@+id/cancel"
                android:layout_marginRight="10dp"
                android:text="Next" />       
       </TableRow>
    </TableLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      有点老了,但我相信我有一些东西要补充。

      我相信根据文档(引用如下),layout_width 和 layout_height 是为所有 TableRow 子项预先确定的。

      相反,layout_weight 可用于确定 TableRow 中元素的宽度/高度。也就是说,除非你想设置它的 android:ems 属性并根据它的字体大小给它一个宽度。

      例如:

      <TableRow
          android:id="@+id/tableRow1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center" >
      
          <EditText
              android:id="@+id/editText1"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:hint="@string/place"
              android:inputType="textPersonName" >
      
              <requestFocus />
          </EditText>
      
      </TableRow>
      

      这为我在 TableRow 中生成了一个全屏宽的 EditText。

      这是来自 TableRow 的documentation 的引用:

      TableRow 的子项不需要指定 layout_width 和 XML 文件中的 layout_height 属性。 TableRow 始终强制执行 这些值分别是 MATCH_PARENT 和 WRAP_CONTENT。

      我们知道,问题在于,实际上它并没有发生。我怀疑 layout_width 也会收到“WRAP_CONTENT”。

      【讨论】:

        【解决方案5】:

        你可以在edittextview中使用layout_weight="1"

        【讨论】:

          【解决方案6】:

          请将android:stretchColumns="0" 添加到您的表格布局中

          【讨论】:

          • 太棒了!这对我来说太完美了!!谢谢
          【解决方案7】:

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/back" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" -->> change to 0dip android:paddingRight="@dimen/activity_horizontal_margin" -->> change to 0dip android:paddingTop="@dimen/activity_vertical_margin" tools:context=".StartUp" >

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多