问题描述
im在TextInputLayout中使用AutoCompleteTextView并借助ArrayAdapter填充它。
val view = TextInputLayout(
ContextThemeWrapper(
this,R.style.AppTheme_TextInput_Dropdown
)
)
val autoCompleteTextView = AutoCompleteTextView(
ContextThemeWrapper(
view.context,R.style.AppTheme_AutoCompleteEditText
)
<setting some parameters here>
val arrayAdapter = ArrayAdapter(
this,android.R.layout.simple_dropdown_item_1line,arrayOf("Temporary","Mocked","Data","Some other data","Testing123"),)
autoCompleteTextView.setAdapter(arrayAdapter)
view.addView(autoCompleteTextView)
return view
效果看起来像这样
但是我想更改dropDown列表上的填充和高度,因此它看起来像这样
还没有找到任何有用的方法/参数来使它前进。有什么简单的方法可以做到这一点,或者将ArrayAdapter更改为其他适配器(例如SpinnerAdapter)可以完成这项工作?
解决方法
请尝试以下操作:
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
public class AutoCompleteDropDownExample extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatAutoCompleteTextView autoCompleteTextView = new AppCompatAutoCompleteTextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
autoCompleteTextView.setLayoutParams(params);
autoCompleteTextView.setDropDownWidth(LinearLayout.LayoutParams.MATCH_PARENT);
autoCompleteTextView.setThreshold(1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,new String[]{"Temporary","Mocked","Data","Some other data","Testing123"});
autoCompleteTextView.setAdapter(adapter);
setContentView(autoCompleteTextView);
}
- 您没有为
AutoCompleteTextView
设置布局参数 -
setDropDownWidth(LinearLayout.LayoutParams.MATCH_PARENT);
将解决问题。
通过添加自己在活动中加载的XML实现,使它看起来更好
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="0dp"
android:paddingTop="0dp"
android:paddingEnd="0dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
然后参加活动
val arrayAdapter = ArrayAdapter(
this,R.layout.dictionary_list_item,arrayOf("Temporary","Testing123"),)
- 像以前的答案/评论中一样更改宽度。