建议下拉列表中的文本大小与其AutoCompleteTextView

时间:2018-08-17 00:53:31

标签: java xml android-layout android-view android-developer-api

我设置的文字大小小于AutoCompleteTextView中的默认文字大小,期望建议的下拉列表具有相同的文字大小和字体。但是,显然是不同的。当我在activity_main.xml中用android:textSize="15dp"设置了EditBox(AutoCompleteTextView)文本大小时,建议列表的文本大小保持不变。这是结果。

enter image description here

我搜索了一个可能控制建议文字大小但找不到的属性。如何确保所有文本的大小,建议和编辑框都相同。

以下是需要时的代码。

public class MainActivity extends AppCompatActivity {
    private static String [] countries = new String[] {"Vatican", "Brazil",
            "Switzerland", "Angola", "Haiti", "Puerto Rico", "Germany", "Mexico",
            "France", "Ireland", "India", "Australia", "Zimbabwe", "South Africa",
            "Canada", "Thailand", "Japan", "Chile", "Botswana", "El Salvador", "Cuba",
            "Vietnam", "Mongolia", "Morocco", "Italy", "Portugal", "Ukrain"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up autocomplete widget
        AutoCompleteTextView countryTV = (AutoCompleteTextView) findViewById(R.id.autoCompleteQuery);

        ArrayAdapter<String> countriesList = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, countries);
        countryTV.setAdapter(countriesList);
}

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee"
    tools:context=".MainActivity">


    <AutoCompleteTextView
        android:id="@+id/autoCompleteQuery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="375dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/white"
        android:hint="Select Country"
        android:padding="10dp"
        android:textSize="15dp"
        android:singleLine="true"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

谢谢

2 个答案:

答案 0 :(得分:0)

对ArrayAdapter的项目使用自定义布局,如下所示:

  

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="15dp"
        android:singleLine="true"
        android:textColor="@android:color/black"
        android:background="@android:color/white"/>
</LinearLayout>

在阵列适配器中设置布局

ArrayAdapter<String> countriesList = new ArrayAdapter<>(this,
               R.layout.item_layout, countries);
countryTV.setAdapter(countriesList);

希望它对您有用。

答案 1 :(得分:0)

android.R.layout.simple_list_item_1使用textAppearance而不是textSize。它使用低于值的值。

android:textAppearance="?android:attr/textAppearanceListItemSmall"

您要么需要将此textAppearance值设置为AutocompleteTextView,要么使用所需的TextView值创建单独的布局,并在设置适配器时将其用作自定义资源来代替android.R.layout.simple_list_item_1