Why EditText is not expanding to fill_parent
-
22-06-2021 - |
سؤال
I have written an XML code, where I want the EditText to be expanded to fit the parent width. After spending so much time, I can not find a way to expand the EditText.
Here is my 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>
what i have made wrong here. Please help me.
المحلول
Add an extra colomn of TextView in every row.
This code may help you.
<?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>
نصائح أخرى
Kind of old, but I believe I have something to add.
I believe that according to the documentation (quoted below) the layout_width and layout_height are pre-determined for all TableRow children.
Instead, the layout_weight can be used to determine the width/height of the elements inside a TableRow. That is, unless you want to set it's android:ems property and give it a width based on it's font size.
For example:
<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>
This produced for me a full-screen wide EditText within a TableRow.
Here's a quote from the documentation of TableRow:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.
The problem is, as we know, that in reality it doesn't happen. I suspect layout_width receives "WRAP_CONTENT" as well.
Please add android:stretchColumns="0"
to your TableLayout
Not sure if serious.
You have your EditText's minimum width (minWidth
) set larger than it's maximum width (maxWidth
), and you haven't given it a layout_width
attribute (which you should set to match_parent
).
I think you are missing the android:orientation
attribute from the LinearLayout
Secondly if this is all what you require, you can avoid using a TableLayout
and use only a single LinearLayout
with android:orientation="vertical"
and all the other three View
s in it.
you can use layout_weight="1"
in edittextview.
<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" >