Android Multiline Edittext在输入文本时缩小/混乱背景

时间:2015-02-11 13:17:43

标签: android android-edittext multiline

我遇到了多行editTextView的问题。我正在将单行edittext视图和多行edittext视图添加到linearlayout(请在xml下面的customlayout linearlayout中引用),并设置background.Everything工作正常但是当文本是在多行edittextview中输入多行edittextview的背景正在缩小并进一步输入二线或三线背景中的文本,并且所有单行编辑文本视图的背景都消失了。请帮我解决这个问题。提前谢谢

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 个答案:

答案 0 :(得分:0)

经过大量的杂耍,我终于解决了这个问题,但我仍然无法弄清楚实际的问题。我做的是我写了一个单独的函数来获取多行文字。它就像一个魅力

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;

}