如何在Android中设置固定大小的编辑框

3

我正在开发一个与Android应用相关的项目,我想要固定编辑框的尺寸。例如,如果我想在上面添加100个字,那么编辑框的大小就不会增加。字符应该保持在框内而不是增加编辑文本的大小。我附带了我的editText的XML代码和图片。

                   <EditText
                            android:id="@+id/fnametxt"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.1"
                            android:background="@color/Beige"
                            android:enabled="false"
                            android:lines="1"
                            android:ems="15"
                            android:maxLines="1"
                            android:scrollHorizontally="true"
                            android:singleLine="true"
                            android:text="ufyufgiuhjlkh"
                            android:textColor="@color/Black"
                            android:textSize="12sp" />

这是图片描述 提前致谢

4个回答

2

调整你的字段宽度。

你可以通过以下几种方式实现:

  1. 使用LinearLayout和权重。容器应该有 android:weightSum="100"(你可以替换成其他数字)。例如,你可以在这里这里查看示例。使用权重时,你应该设置 android:layout_width="0dp" 以查看结果。

  2. 使用RelativeLayout和边距。将第一个EditText设置为 android:layout_alignParentRight="true"android:layout_toRightOf="IdOfTextView"。将右边距设置为(例如)10dp。EditField将被拉伸以填充所有空闲空间。

  3. 你可以将 android:maxWidth 设置为某个值(以dp或px为单位)。或者 maxLengthmaxEms 可以解决问题。


1

变更:

android:layout_width="wrap_content"

转换成中文为:

转化成类似这样的

android:layout_width="200dp"

或者您选择任何大小。

0
如果您打算使用layout_weight属性设置高度,请设置您的。
layout_height="0"

否则设置

layout_height="100dp" 

或者您可以根据需要设置固定高度。


0
也许您需要使用XML中的android:maxLines和android:lines属性,并操作maxLength。
<EditText
          android:id="@+id/fnametxt"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1.1"
          android:background="@color/Beige"
          android:enabled="false"
          android:lines="5"
          android:ems="15"
          android:maxLines="100"
          android:scrollHorizontally="true"
          android:singleLine="true"
          android:text="ufyufgiuhjlkh"
          android:textColor="@color/Black"
          android:textSize="12sp" />

来源:Android开发者最大行数


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接