Android Multiline Edittext shrinks /disorients bac

2019-09-04 07:42发布

问题:

I am facing issue with the multiline editTextView.I am adding single line edittext views and multiline edittext views to linearlayout(Please refer in customlayout linearlayout from below xml) programetically and also setting background.Everything working works fine but when the text is entered inside multiline edittextview the background of multiline edittextview is shrinking and further entering texts in in 2nd or 3rd line background disoriening and also background of all single line edittextview disappearing.Please help me for this issue..Thanks in advance

private EditText getEditText(String sHint,Boolean isMultiLine,Drawable drwable)
{
    EditText editTxt;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.topMargin=2;
    params.leftMargin=40;
    params.rightMargin=20;
    params.bottomMargin=10;
    params.gravity = Gravity.CENTER_HORIZONTAL;
    editTxt = new EditText(this);
    editTxt.setLayoutParams(params);
    editTxt.setTextAppearance(this, android.R.style.TextAppearance_Medium);
    editTxt.setHint(sHint);
    editTxt.setBackground(drwable);
    editTxt.setGravity(Gravity.CENTER);
    editTxt.setTextColor(Color.WHITE);
    editTxt.setTypeface(null, Typeface.ITALIC);

    if(isMultiLine==true)
    {
        editTxt.setLines(3);
        editTxt.setSingleLine(false); 
        editTxt.setInputType(EditorInfo.TYPE_CLASS_TEXT|EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
    }
    else
    {
        editTxt.setLines(1);
        editTxt.setSingleLine(true); 
    }



        return editTxt;

}

<!-  Backgroung Xml->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape>
        <solid android:color="@color/red" />
    </shape>
</item>

<item
    android:bottom="1px"
    android:left="0px">
    <shape android:shape="rectangle" >
        <solid android:color="#000000" />
    </shape>
</item>

</layer-list>


<!-  LayoutXml->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".Activity_CustomCatAddForm" >

     <RelativeLayout
         android:id="@+id/header"
         android:layout_width="match_parent"
         android:layout_height="45dp"
         android:layout_alignParentTop="true"
         android:background="@drawable/headertheme1" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
             android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/txtDlg1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/image"
            android:shadowColor="@color/black"
            android:shadowDx="2"
            android:shadowDy="2"
            android:shadowRadius="2"
            android:text="Custom info"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/gold"
            android:textStyle="italic" 

            android:layout_marginLeft="30dp"/>

          <TextView
            android:id="@+id/txtTicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

             android:layout_centerVertical="true"
             android:text=""
             android:layout_alignParentRight="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/blue" />

  </RelativeLayout>  

              <RelativeLayout
                  android:id="@+id/footer"
                  android:layout_width="fill_parent"
                  android:layout_height="50dp"
                  android:layout_alignParentBottom="true"
                  android:background="@drawable/headertheme1" >

               <ImageButton
                   android:id="@+id/imgDeletBtn"
                   android:layout_width="40dp"
                   android:layout_height="wrap_content"
                   android:layout_alignParentLeft="true"
                   android:layout_centerHorizontal="true"
                   android:layout_marginLeft="30dp"
                   android:background="@drawable/back_ground_transparent"
                   android:onClick="imgdeltebtn_onclick"
                   android:scaleType="centerInside"
                   android:src="@drawable/bin" />

                <ImageButton
                    android:id="@+id/imgCloseBtn"
                    android:layout_width="45dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginRight="30dp"
                    android:background="@drawable/back_ground_transparent"
                    android:onClick="imgclosebtn_onclick"
                    android:scaleType="centerInside"
                    android:src="@drawable/close" />

                <ImageButton
                    android:id="@+id/imgSaveBtn"
                    android:layout_width="40dp"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@drawable/back_ground_transparent"
                    android:onClick="imgsavebtn_onclick"
                    android:scaleType="centerInside"
                    android:src="@drawable/save"
                    android:tag="save" />

    </RelativeLayout>
       <ScrollView
         android:id="@+id/scrlViewx"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fillViewport="true"
         android:background="@color/titlebackgroundcolor" 
         android:layout_above="@+id/footer"
         android:layout_below="@+id/header"
         >

        <LinearLayout
            android:id="@+id/customlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bodytheme1"
            android:clickable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true" >

            <TextView
                android:id="@+id/txtFile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_marginLeft="5dp"
                android:text="Record short-name :"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/blue"
                android:drawablePadding="10dp"/>
              <EditText
                  android:id="@+id/txtTypeName"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:background="@drawable/underline"
                  android:gravity="center"
                  android:hint="Enter Name"
                  android:maxLength="15"
                  android:singleLine="true"
                  android:tag="TypeName"
                  android:textColor="@color/white"
                  android:textStyle="italic" />

       </LinearLayout>

 </ScrollView>


</RelativeLayout>

回答1:

After lot of juggling finally i solved the issue but still i could not figured-out the actual issue.What i did is i wrote a saperate function to get multiline text. it worked like a charm

private EditText getMultiLineEditText(String sHint,int id,Drawable drwable,String tag,int fldType,int fldFlag)
{
    EditText editTxt;
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    //   params.gravity = Gravity.CENTER;
    params.topMargin=2;
    params.leftMargin=40;
    params.rightMargin=20;
    params.bottomMargin=10;
    params.weight=1;
    params.gravity = Gravity.CENTER_HORIZONTAL;
    editTxt = new EditText(this);
    editTxt.setLayoutParams(params);
    editTxt.setId(id);
    editTxt.setInputType(EditorInfo.TYPE_CLASS_TEXT|EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
    editTxt.setLines(3);
    editTxt.setMaxLines(3);
    editTxt.setSingleLine(false);
    editTxt.setTextAppearance(this, android.R.style.TextAppearance_Medium);
    editTxt.setHint(sHint);
    editTxt.setContentDescription(1+"");
    editTxt.setBackground( getResources().getDrawable(R.drawable.rcornerx));
    editTxt.setGravity(Gravity.CENTER);
    editTxt.setTextColor(Color.WHITE);
    editTxt.setTypeface(null, Typeface.ITALIC);
    editTxt.setTag(id+"-"+tag);
    editTxt.setCompoundDrawablePadding(10);



        return editTxt;

}