为什么EditText没有扩展到fill_rent



我已经编写了一个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>

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

有点老了,但我相信我还有需要补充的地方。

我相信,根据下面引用的文档,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:的文档

TableRow的子级不需要指定layout_width和layout_height属性。TableRow始终强制这些值分别是MATCH_PARENT和WRAP_CONTENT。

问题是,正如我们所知,事实上它并没有发生。我怀疑layout_width也收到了"WRAP_CONTENT"。

请将android:stretchColumns="0"添加到您的TableLayout

在每行中添加一列额外的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>

不确定是否严重。

您已将EditText的最小宽度(minWidth)设置为大于其最大宽度(maxWidth),并且尚未为其指定layout_width属性(应将其设置为match_parent)。

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

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

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

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

最新更新