gpt4 book ai didi

java - 添加数组列表时的Android Material Design下拉布局问题

转载 作者:行者123 更新时间:2023-12-05 00:19:26 24 4
gpt4 key购买 nike

我在使用 android material design 下拉菜单项时遇到了一些问题。以下是我的代码和图像问题。我实现了 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 之后的布局:
enter image description here
相同的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);
下拉项.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代码宽度自动增加。现在我的布局看起来像这样。不明白为什么对齐会受到干扰。:
enter image description here
另外请帮助为什么在产品类别布局中我选择的项目超出了提示。为什么它不像编辑文本那样采用相等的间距。同样的问题也与重量单位布局有关。比较产品重量文本和重量单位文本,对齐方式不同。此外,当我单击此下拉菜单时,我的键盘会打开。我不想要键盘。

最佳答案

有同样的问题和 the documentation解释说,您最好在 TextInputLayout 中使用样式为 ExposedDropdownMenus 实现正确的效果和行为如下:

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
也适用于 TextInputLayoutTextInputEditText :
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
结果:
enter image description here
代码:
<?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可以通过使用 onFocusChangeListener 的技巧来解决的菜单(它不应该得到任何值等)要告诉用户何时点击小部件并获得焦点,请隐藏键盘并使其仅显示列表:
(state_layout.editText as AutoCompleteTextView).inputType = EditorInfo.TYPE_NULL
state_layout.onFocusChangeListener = View.OnFocusChangeListener { _: View?, hasFocus ->
if (hasFocus) {
hideKeyboard(this)
}
}

关于java - 添加数组列表时的Android Material Design下拉布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62888140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com