简体   繁体   中英

how to restrict the width of an editText

in the below posted layout, the editText is always strectch up to the end even when i restrict its width to be something like 30dp i want it to be not stretched upto the end, how can i do so

*XML Code :

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top|left"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:src="@android:drawable/alert_light_frame"/>
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:text="roll: "/>
    <EditText 
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:hint="E"
        android:ems="6"/>
</TableRow>

Use the android:maxWidth attribute:

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:hint="E"
    android:ems="6"
    android:maxWidth="30dp"/>

Always Use wrap_content instead of any value inside Edittext, Textview, etc. and try to use "sp" instead of dp. Try below code:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:hint="E"
    android:ems="6" />

Never define table row inside table layout in XML, The more preferable method is to create rows dynamically as below, Try below code.

TableLayout tl;
tl = (TableLayout) findViewById(R.id.mainTable)
tr = new TableRow(this);

label = new TextView(this);
label.setText("GFD");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.parseColor("#FF1493"));
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll);

tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

Try to use wrap_content for your TableLayout s or TableRow s width:

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

    <TableRow 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|left"
    android:orientation="horizontal">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM