![](/img/trans.png)
[英]Dynamically added TextInputLayout is not shown in LinearLayout
[英]How to add TextInputLayout inside the LinearLayout by dynamically?
我想動態地在線性布局中添加 TextInputLayout。 我可以在 linearLayout 中添加 TextView 和 EditText,但無法添加 TextInputLayout。 那是我的線性布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:id="@+id/userInformation_lnr">
我可以使用以下代碼塊添加 TextView 和 EditText
LinearLayout ll = (LinearLayout) findViewById(R.id.userInformation_lnr);
for(int i = 0; i <oyuncuSayisi; i++ ){
EditText et = new EditText(this);
TextView tx = new TextView(this);
tx.setText("Player "+(i+1));
tx.setTextSize(19);
tx.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
tx.setTextColor(this.getResources().getColor(R.color.silver));
et.setHint((i+1)+".Enter Player Name");
et.setHintTextColor(this.getResources().getColor(R.color.DarkGoldenrod));
DrawableCompat.setTint(et.getBackground(), ContextCompat.getColor(this, R.color.silver));
et.setTextColor(this.getResources().getColor(R.color.SteelBlue));
et.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
et.setTextSize(17);
et.setId((i+1));
ll.addView(tx);
ll.addView(et);
}
但是我不知道如何添加TextInputLayout,如何在LinearLayout里面添加TextInputLayout?
TextInputLayout 需要一個 TextInputEditText 才能工作,您可以為您的 TextInputLayout 創建一個自定義視圖,然后將其添加到 LinearLayout。
<com.google.android.material.textfield.TextInputLayout 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:id="@+id/tv_input_layout"
style="@style/YourCustomStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:hint="hint"
tools:placeholderText="ex: hint">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tv_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="value" />
</com.google.android.material.textfield.TextInputLayout>
class:
class CustomTextInputLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {
init {
View.inflate(context, R.layout.custom_input_layout, this)
// set the other things you need
}
}
然后將其添加到線性布局中:
private fun createInput() {
val view = CustomTextInputLayout(context)
ll.addView(view)
}
}
如果將演示文稿移至 xml 文件,則可以解耦演示文稿和業務邏輯。
input_layout.xml
<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="@style/MyInputTheme" <-- You can define here your theme or style
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
並通過LayoutInflater
創建:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater.from(context).inflate(R.layout.input_layout, container, true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.