Android material 添加数组列表时设计下拉布局问题
Android material design drop-down layout issue when array list added
我在 android material 设计下拉菜单项时遇到了一些问题。以下是我的代码和图像问题。我实现了 material 设计下拉菜单项,之前的布局非常好。但在添加数组列表后,我的布局宽度会自动增加。下面是我的代码:
XML:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="Product Category"
app:endIconMode="dropdown_menu"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/productCategoryDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@null"
android:paddingStart="10dp" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:layout_weight="1.115"
android:hint="Product Weight"
app:hintTextColor="@color/colorPrimary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:textCursorDrawable="@null" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="0.9"
android:hint="Weight Unit"
app:endIconMode="dropdown_menu"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/productWeightUnitDropdown"
android:paddingStart="5dp"
android:textCursorDrawable="@null"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
我在 XML 之后的布局:
Java 相同的代码:
Dropdown = view.findViewById(R.id.productWeightUnitDropdown);
String[] weightUnit = new String[] {
"kg (Kilogram)",
"pcs (Piece)"
};
ArrayAdapter<String> weightUnitAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.dropdown_item,
weightUnit
);
weightUnitDropdown.setAdapter(weightUnitAdapter);
下拉Item.XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1">
</TextView>
添加java代码后重量单位下拉布局的宽度自动增加。现在我的布局看起来像这样。不明白为什么对齐会受到干扰。:
另外请帮忙说明为什么在产品分类布局中我选择的商品会提示过来。为什么它不像编辑文本那样采用等间距。重量单位布局也存在同样的问题。比较产品重量文本和重量单位文本,不在同一对齐方式。此外,当我单击此下拉菜单时,我的键盘会打开。我不想要键盘。
遇到了同样的问题,正如 the documentation 所解释的那样,您最好在 TextInputLayout
中使用一种样式来为 ExposedDropdownMenus
实现正确的效果和行为,如下所示:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
还有 TextInputLayout
和 TextInputEditText
:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
结果:
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="Product Category"
app:endIconMode="dropdown_menu"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/productCategoryDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:textCursorDrawable="@null" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:hint="Product Weight"
app:hintTextColor="@color/colorPrimary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:hint="Weight Unit"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/productWeightUnitDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
P.S:无需提及 AutoCompleteTextView
存在一些行为问题,例如当您点击小部件时,它会显示 DropDown
菜单(它不应该t 得到任何值等)这可以通过使用 onFocusChangeListener
的技巧来解决,告诉用户何时点击小部件并获得焦点,隐藏键盘并使其仅显示列表:
(state_layout.editText as AutoCompleteTextView).inputType = EditorInfo.TYPE_NULL
state_layout.onFocusChangeListener = View.OnFocusChangeListener { _: View?, hasFocus ->
if (hasFocus) {
hideKeyboard(this)
}
}
我在 android material 设计下拉菜单项时遇到了一些问题。以下是我的代码和图像问题。我实现了 material 设计下拉菜单项,之前的布局非常好。但在添加数组列表后,我的布局宽度会自动增加。下面是我的代码:
XML:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="Product Category"
app:endIconMode="dropdown_menu"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/productCategoryDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@null"
android:paddingStart="10dp" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:layout_weight="1.115"
android:hint="Product Weight"
app:hintTextColor="@color/colorPrimary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:textCursorDrawable="@null" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="0.9"
android:hint="Weight Unit"
app:endIconMode="dropdown_menu"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/productWeightUnitDropdown"
android:paddingStart="5dp"
android:textCursorDrawable="@null"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
我在 XML 之后的布局:
Java 相同的代码:
Dropdown = view.findViewById(R.id.productWeightUnitDropdown);
String[] weightUnit = new String[] {
"kg (Kilogram)",
"pcs (Piece)"
};
ArrayAdapter<String> weightUnitAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.dropdown_item,
weightUnit
);
weightUnitDropdown.setAdapter(weightUnitAdapter);
下拉Item.XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1">
</TextView>
添加java代码后重量单位下拉布局的宽度自动增加。现在我的布局看起来像这样。不明白为什么对齐会受到干扰。:
另外请帮忙说明为什么在产品分类布局中我选择的商品会提示过来。为什么它不像编辑文本那样采用等间距。重量单位布局也存在同样的问题。比较产品重量文本和重量单位文本,不在同一对齐方式。此外,当我单击此下拉菜单时,我的键盘会打开。我不想要键盘。
遇到了同样的问题,正如 the documentation 所解释的那样,您最好在 TextInputLayout
中使用一种样式来为 ExposedDropdownMenus
实现正确的效果和行为,如下所示:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
还有 TextInputLayout
和 TextInputEditText
:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
结果:
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="Product Category"
app:endIconMode="dropdown_menu"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/productCategoryDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:textCursorDrawable="@null" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:hint="Product Weight"
app:hintTextColor="@color/colorPrimary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:hint="Weight Unit"
app:hintTextColor="@color/colorPrimary">
<AutoCompleteTextView
android:id="@+id/productWeightUnitDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
P.S:无需提及 AutoCompleteTextView
存在一些行为问题,例如当您点击小部件时,它会显示 DropDown
菜单(它不应该t 得到任何值等)这可以通过使用 onFocusChangeListener
的技巧来解决,告诉用户何时点击小部件并获得焦点,隐藏键盘并使其仅显示列表:
(state_layout.editText as AutoCompleteTextView).inputType = EditorInfo.TYPE_NULL
state_layout.onFocusChangeListener = View.OnFocusChangeListener { _: View?, hasFocus ->
if (hasFocus) {
hideKeyboard(this)
}
}