![](/img/trans.png)
[英]Android: Change hint text color for a material exposed outlined drop-down when unfocused
[英]Android material design drop-down layout issue when array list added
我在使用 android 材料設計下拉菜單項時遇到了一些問題。 以下是我的代碼和圖像問題。 我實現了材料設計下拉菜單項,之前的布局非常好。 但是在添加數組列表后,我的布局寬度會自動增加。 以下是我的代碼:
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);
下拉項目.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代碼后重量單位下拉布局的寬度自動增加。 現在我的布局看起來像這樣。 不明白為什么 alignment 會受到干擾。:
另外請幫助為什么在產品類別布局中我選擇的項目超出了提示。 為什么它不像編輯文本那樣采用相等的間距。 同樣的問題也與重量單位布局有關。 比較產品重量文字和重量單位文字,不在同一個alignment。 此外,當我單擊此下拉菜單時,我的鍵盤會打開。 我不想要鍵盤。
有同樣的問題,正如文檔解釋的那樣,您最好在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>
PS:無需提及AutoCompleteTextView
存在一些行為問題,例如當您點擊小部件時,它會顯示DropDown
菜單(並且它不應該獲得任何值等),這可以通過使用onFocusChangeListener
的技巧來解決要告訴用戶何時點擊小部件並獲得焦點,請隱藏鍵盤並使其僅顯示列表:
(state_layout.editText as AutoCompleteTextView).inputType = EditorInfo.TYPE_NULL
state_layout.onFocusChangeListener = View.OnFocusChangeListener { _: View?, hasFocus ->
if (hasFocus) {
hideKeyboard(this)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.