我有一个标签和一个输入字段(const theRest = sliced.map((item, i) => {
item[i].name;
})
),我以编程方式添加到EditText
。当我输入比输入字段长的文本时,值将偏离屏幕。我应该如何解决这个问题,以便我可以看到我正在键入的内容,还可以回滚到输入字段的开头?
键入TableLayout
时的示例:
布局XML:
A-Z1-9
代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.DocumentActivity">
<TableLayout
android:id="@+id/document_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TableLayout>
答案 0 :(得分:0)
嗨,你必须删除这一行
inputField.maxLines = 1
并使用LayoutParams
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
inputField.setLayoutParams(params);
希望它会对你有所帮助:)。
答案 1 :(得分:0)
我已设法使用垂直LinearLayout
组件作为主占位符来实现此功能,然后使用LinearLayout
和ViewText
组件填充水平EditText
组件。< / p>
我生成15个'标签'和'输入字段'的完整实现是:
布局XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.DocumentActivity"
tools:layout_editor_absoluteY="81dp"
android:id="@+id/activity_document">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/document_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
<强>代码:强>
var documentLayout: LinearLayout = findViewById(R.id.document_layout)
var lp = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
lp.leftMargin = 8
lp.rightMargin = 8
for (i in 0..15) {
var linearLayout = LinearLayout(this)
linearLayout.id = View.generateViewId()
linearLayout.layoutParams = lp
val inputField = EditText(this)
inputField.id = View.generateViewId()
inputField.setSingleLine()
inputField.layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT, 1f)
val label = TextView(this)
label.id = View.generateViewId()
label.text = "Label $i"
label.layoutParams = LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT, 0f)
linearLayout.addView(label)
linearLayout.addView(inputField)
documentLayout.addView(linearLayout)
}