2012-07-22 8 views
9

Ich habe einen XML-Code geschrieben, wo ich den EditText erweitern soll, um ihn an die Elternbreite anzupassen. Nach so viel Zeit kann ich keinen Weg finden, den EditText zu erweitern.Warum EditText nicht zu fill_parent expandiert

Hier ist meine 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> 

, was ich hier falsch gemacht haben. Bitte hilf mir.

Antwort

2

Fügen Sie eine zusätzliche Spalte von TextView in jeder Zeile hinzu.

Dieser Code kann Ihnen helfen.

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

Nicht sicher, ob ernst.

Sie haben Ihre EditText die Mindestbreite (minWidth) größer eingestellt als es maximale Breite (maxWidth) ist, und Sie haben es nicht ein layout_width Attribut gegeben (die Sie auf match_parent gesetzt sollte).

+0

ich es eine layout_width gegeben haben match_parent, aber nichts ändert sich. – user1462473

+0

Entfernen Sie auch die Attribute "maxWidth" und "minWidth". – kcoppock

0

Ich glaube, Sie fehlt das android:orientation Attribut aus dem LinearLayout

Zweitens, wenn das alles ist, was Sie benötigen, können Sie nur eine einzige LinearLayout mit android:orientation="vertical" und all die anderen drei View s TableLayout und verwenden vermeiden, mit in es.

+0

Das 'android: orientation' ist nicht zwingend erforderlich. Laut http://developer.android.com/reference/android/widget/LinearLayout.html ist es standardmäßig "horizontal". –

8

Art alt, aber ich glaube, ich habe etwas hinzuzufügen.

Ich glaube, dass laut der unten aufgeführten Dokumentation die Layout-Breite und die Layout-Höhe für alle TableRow-Kinder festgelegt sind.

Stattdessen kann die layout_weight verwendet werden, um die Breite/Höhe der Elemente in einem TableRow zu bestimmen. Das heißt, es sei denn, Sie möchten die android: ems-Eigenschaft festlegen und ihr eine Breite zuweisen, die auf der Schriftgröße basiert.

Zum Beispiel:

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

Das ist für mich produziert eine Vollbild-weiten EditText innerhalb eines TableRow.

Hier ist ein Zitat aus dem documentation von TableRow:

Die Kinder eines TableRow brauchen nicht die layout_width und angeben layout_height in der XML-Datei-Attribute. TableRow erzwingt immer diese Werte jeweils MATCH_PARENT und WRAP_CONTENT.

Das Problem ist, wie wir wissen, dass es in Wirklichkeit nicht passiert. Ich vermute, dass layout_width auch "WRAP_CONTENT" empfängt.

0

können Sie layout_weight="1" in Edittextview verwenden.

6

Bitte fügen android:stretchColumns="0" zu Ihrem Tablelayout

+0

Super! Das ist perfekt für mich !! Danken – arttioz

0

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