Android 我应该如何增加微调列表项的字体大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10409871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:36:28  来源:igfitidea点击:

How should I increase the spinner list item font size?

androidspinner

提问by divaNilisha

This is my spinner's code:

这是我的微调器的代码:

Spinner food = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> foodadapter = ArrayAdapter.createFromResource(
        this, R.array.item_array, android.R.layout.simple_spinner_item);
foodadapter.setDropDownViewResource(android.R.layout.simple_spinner_item);        
food.setAdapter(foodadapter);

This is a part of the layout file where I have created the spinner:

这是我创建微调器的布局文件的一部分:

<Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView1" />

This is the item_array:

这是item_array

<resources>
    <string name="item_picker">Select an iten</string>
    <string-array name="item_array">
        <item>Pizza</item>
        <item>Burger</item>
        <item>Sandwiches</item>
        <item>Bread</item>
        <item>Pastries</item>
        <item>Snackers</item>
    </string-array>
</resources>

Note: I do not want a simple_spinner_dropdown_item.

注意:我不想要一个simple_spinner_dropdown_item.

I just want to increase the size of the list items. How can I do that?

我只想增加列表项的大小。我怎样才能做到这一点?

回答by V.J.

Save the below xml as spinner_layout.xml in layout folder

将以下 xml 保存为布局文件夹中的 spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/spinnerTarget"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#000000"
          android:textSize="13sp"
/>

change the textSize which you want. and use the below adapter code to fill it.

更改您想要的 textSize 。并使用下面的适配器代码来填充它。

Spinner food = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> foodadapter = ArrayAdapter.createFromResource(
            this, R.array.item_array, R.layout.spinner_layout);
foodadapter.setDropDownViewResource(R.layout.spinner_layout);
food.setAdapter(foodadapter);

回答by Joshua Pinter

Via XML Only

仅通过 XML

Just to help others in case they are statically setting their Spinner entries in XML.

只是为了帮助其他人,以防他们在 XML 中静态设置 Spinner 条目。

The above answers work if you're creating your Spinnervia code but if you're setting your Spinner entries via XML, i.e. using android:entries, then you can adjust the text size and other attributes with the following two theme settings:

如果您通过代码创建Spinner,则上述答案有效,但如果您通过 XML 设置 Spinner 条目,即使用android:entries,则可以使用以下两个主题设置调整文本大小和其他属性:

In your res/values/styles.xml

在你的 res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme" parent="android:Theme.Holo">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

        <!-- For the resting Spinner style -->
        <item name="android:spinnerItemStyle">
            @style/spinnerItemStyle
        </item> 

        <!-- For each individual Spinner list item once clicked on -->
        <item name="android:spinnerDropDownItemStyle">
            @style/spinnerDropDownItemStyle
        </item>

    </style>

    <style name="spinnerItemStyle">
        <item name="android:padding">10dp</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="spinnerDropDownItemStyle">
        <item name="android:padding">20dp</item>
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

</resources>

回答by Diiiiii

The above solutions are all hard coded in the xml.

以上解决方案都是在xml中硬编码的。

There is an alternative solution which allows you to change it programmatically. https://stackoverflow.com/a/11494962/5089713Once you have the TextView, you can do whatever with it, for instance change the font size.

有一个替代解决方案允许您以编程方式更改它。 https://stackoverflow.com/a/11494962/5089713拥有 TextView 后,您可以对其进行任何操作,例如更改字体大小。

your code then looks like

你的代码看起来像

Spinner food = (Spinner) findViewById(R.id.spinner1);

CharSequence[] strings = getActivity().getResources().getTextArray(R.array.item_array);
    ArrayAdapter<CharSequence> foodadapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, strings){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view =super.getView(position, convertView, parent);
            TextView textView=(TextView) view.findViewById(android.R.id.text1);
            // do whatever you want with this text view
            textView.setTextSize(20);
            return view;
        }
    };

foodadapter.setDropDownViewResource(android.R.layout.simple_spinner_item);        
food.setAdapter(foodadapter);

回答by David Kariuki

private OnItemSelectedListener spinner = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

       ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
       ((TextView) parent.getChildAt(0)).setTextSize(10);

    }

    public void onNothingSelected(AdapterView<?> parent) {

    }
};

回答by Sean Stayns

If you only want to increase the size, that the dropdown is good touchable, then you simply can change the simple_spinner_itemto simple_spinner_dropdown_item:

如果您只想增加大小,下拉菜单是可触摸的,那么您只需将simple_spinner_item更改为simple_spinner_dropdown_item

 ArrayAdapter.createFromResource(this, R.array.item_array, android.R.layout.simple_spinner_dropdown_item);
                                                                       ---------------------------^