Android 多行 Edittext 在输入文本时缩小/迷惑背景
Android Multiline Edittext shrinks /disorients background when text entered
我面临多行问题 editTextView.I 我正在向线性布局添加单行编辑文本视图和多行编辑文本视图(请参考下面的自定义布局线性布局 xml)并设置 background.Everything 工作正常,但是当在多行 edittextview 中输入文本时,多行 edittextview 的背景正在缩小,并在第 2 行或第 3 行背景中进一步输入文本迷失方向,所有单行 edittextview 的背景 disappearing.Please 帮助我解决这个问题问题..提前致谢
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>
经过大量的尝试,我终于解决了这个问题,但我仍然无法弄清楚实际的 issue.What 我所做的是我编写了一个单独的函数来获取多行文本。它就像一个魅力
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;
}
我面临多行问题 editTextView.I 我正在向线性布局添加单行编辑文本视图和多行编辑文本视图(请参考下面的自定义布局线性布局 xml)并设置 background.Everything 工作正常,但是当在多行 edittextview 中输入文本时,多行 edittextview 的背景正在缩小,并在第 2 行或第 3 行背景中进一步输入文本迷失方向,所有单行 edittextview 的背景 disappearing.Please 帮助我解决这个问题问题..提前致谢
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>
经过大量的尝试,我终于解决了这个问题,但我仍然无法弄清楚实际的 issue.What 我所做的是我编写了一个单独的函数来获取多行文本。它就像一个魅力
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;
}