我正在 android 上创建一个可拖动的 EditText。我将editText放在相对布局上,我的问题是每次输入长文本时它都会进入第二行,即使它没有填充到相对布局中。
这是我的示例布局:
+----------------------------------------------------+
| |
| +----------------+ |
| | This is my text| |
| | in editText | |
| +----------------+ |
| |
| |
| |
+----------------------------------------------------+
这就是我想要实现的目标:
+----------------------------------------------------+
| |
| +----------------------------+ |
| | This is my text in editText| |
| +----------------------------+ |
| |
| |
| |
| |
+----------------------------------------------------+
在转到第二行之前,我想先将我的文本包装在整个布局(宽度)中。
这是我的代码:
class AddTextTouchListener implements OnClickListener
{
@Override
public void onClick(View view) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.imgstage);
Context context = getApplicationContext();
EditText imv=new EditText(context);
imv.setText("Sample");
imv.setTextColor(Color.BLACK);
imv.setBackgroundColor(Color.TRANSPARENT);
imv.setCursorVisible(false);
imv.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
imv.setOnLongClickListener(new longClickTextListener());
imv.setEms(10);
imv.setMaxLines(4);
imv.setHorizontallyScrolling(false);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imv.setLayoutParams(params);
layout.addView(imv);
}
}
这是我的 longClickListener 能够拖动editText:
class longClickTextListener implements OnLongClickListener
{
@Override
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.VISIBLE);
lastImageSelected = view;
return true;
}
}