更改微调风格的工具栏

更改微调风格的工具栏

本文介绍了更改微调风格的工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个微调在我的工具栏像老动作条式的导航,我的主题是这样的

 <样式名称=AppBaseTheme父=Theme.AppCompat.Light.NoActionBar>
    <项目名称=colorPrimary> @色/ color_primary< /项目>
    <项目名称=colorPrimaryDark> @色/ color_primary_dark< /项目>
    <项目名称=colorAccent> @色/ color_primary< /项目>
< /风格>
 

但我的微调是黑色,而其他所有的图标和溢出菜单是白色的,所以它看起来不错

我试图改变微调的使用风格这个

 <样式名称=ToolbarSpinnerTheme父=Theme.AppCompat>
    <项目名称=机器人:spinnerItemStyle> @风格/ TextAppearanceSpinnerItem< /项目>
< /风格>

<样式名称=TextAppearanceSpinnerItem>
    <项目名称=机器人:文字颜色>#FFFFFF< /项目>
< /风格>
 

这是我的工具栏是如何风格

 < android.support.v7.widget.Toolbar
           机器人:ID =@ + ID /工具栏
           机器人:layout_width =match_parent
           机器人:layout_height =WRAP_CONTENT
           机器人:=了minHeight?ATTR / actionBarSize
           机器人:ATTR / colorPrimary后台=
           应用程序:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
           应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light>

           <微调
               机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:ID =@ + ID /模式
               机器人:=了minWidth150dp
               机器人:重力=底
               风格=@风格/ ToolbarSpinnerTheme/>

       < /android.support.v7.widget.Toolbar>


最后的微调模式=(微调)findViewById(R.id.modes);

    SpinnerAdapter mSpinner = ArrayAdapter.createFromResource(这一点,R.array.action_bar_spinner,android.R.layout.simple_spinner_dropdown_item);
    mode.setAdapter(mSpinner);
 

但它始终保持黑色。我怎样才能改变微调箭头和文字为白色,同时仍保持相同的主题为下拉式的,你会获得与主题?

更新4.4箭头FIX:

我的箭头变成白色的唯一方法就是以编程方式,而不是加微调的XML,所以它看起来是这样的。

 最后ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(getSupportActionBar()。getThemedContext()
        R.array.main_navigation_list,R.layout.spinner_text);
spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
mNavigationTags = getResources()getStringArray(R.array.main_navigation_list)。


mNavigationSpinner =新的微调(getSupportActionBar()getThemedContext());
mNavigationSpinner.setAdapter(spinnerAdapter);

mNavigationSpinner.setOnItemSelectedListener(本);
mToolbar.addView(mNavigationSpinner)
 

解决方案

当你创建你应该做getApplicationContext而不是此一个ArrayAdapter:

  SpinnerAdapter mSpinner = ArrayAdapter.createFromResource(getApplicationContext(),R.array action_bar_spinner,android.R.layout.simple_spinner_dropdown_item。);
 

请一个新的布局文件:

 < CheckedTextView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                 机器人:ID =@机器人:ID / text1中
                 风格=机器人:ATTR / spinnerDropDownItemStyle
                 机器人:单线=真
                 机器人:layout_width =match_parent
                 机器人:layout_height =机器人:ATTR /列表preferredItemHeight
                 机器人:ellipsize =金字招牌
                 机器人:文字颜色=#000000/>
 

然后改变你的code到这一点:

  ArrayAdapter mAdapter = ArrayAdapter.createFromResource(getApplicationContext(),R.array action_bar_spinner,android.R.layout.simple_spinner_dropdown_item。);
        mAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        mode.setAdapter(mAdapter);
 


您是否尝试过把微调在XML文件中是这样的:

 < android.support.v7.widget.Toolbar
        机器人:ID =@ + ID /工具栏
        机器人:layout_height =?ATTR / actionBarSize
        机器人:layout_width =match_parent
        机器人:后台=>中ATTR / colorPrimary?

    <微调
            机器人:ID =@ + ID / spinner_nav
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT/>

< /android.support.v7.widget.Toolbar>
 

和也禁止这样的标题:

 工具栏= findViewById(R.id.toolbar);
setSupportActionBar(工具栏);
getSupportActionBar()setDisplayShowTitleEnabled(假)。
 

答案是克里斯·巴内斯:http://stackoverflow.com/a/26511653/2767703

I am trying to put a spinner in my Toolbar like the old ActionBar style navigation and my theme is this

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primary_dark</item>
    <item name="colorAccent">@color/color_primary</item>
</style>

but my spinner is black while all other icons and overflow menus are white so it looks bad

I tried changing the style of the spinner using this

<style name="ToolbarSpinnerTheme" parent="Theme.AppCompat">
    <item name="android:spinnerItemStyle">@style/TextAppearanceSpinnerItem</item>
</style>

<style name="TextAppearanceSpinnerItem">
    <item name="android:textColor">#FFFFFF</item>
</style>

this is how my Toolbar is styled

<android.support.v7.widget.Toolbar
           android:id="@+id/toolbar"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:minHeight="?attr/actionBarSize"
           android:background="?attr/colorPrimary"
           app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
           app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

           <Spinner
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/modes"
               android:minWidth="150dp"
               android:gravity="bottom"
               style="@style/ToolbarSpinnerTheme"/>

       </android.support.v7.widget.Toolbar>


final Spinner mode = (Spinner)findViewById(R.id.modes);

    SpinnerAdapter mSpinner = ArrayAdapter.createFromResource(this, R.array.action_bar_spinner, android.R.layout.simple_spinner_dropdown_item);
    mode.setAdapter(mSpinner);

but it always stays black. How can I change the spinner arrow and text to white while still keeping the same theme for the dropdown style as you would get with the Light theme?

Update 4.4 arrow fix:

The only way I got the arrow to turn white is to add the spinner programatically and not in xml so it looks something like this

final ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(),
        R.array.main_navigation_list, R.layout.spinner_text);
spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
mNavigationTags = getResources().getStringArray(R.array.main_navigation_list);


mNavigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
mNavigationSpinner.setAdapter(spinnerAdapter);

mNavigationSpinner.setOnItemSelectedListener(this);
mToolbar.addView(mNavigationSpinner)
解决方案

When you create the arrayadapter you should do getApplicationContext instead of this:

SpinnerAdapter mSpinner = ArrayAdapter.createFromResource(getApplicationContext(), R.array. action_bar_spinner, android.R.layout.simple_spinner_dropdown_item);

Make a new layout file:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@android:id/text1"
                 style="?android:attr/spinnerDropDownItemStyle"
                 android:singleLine="true"
                 android:layout_width="match_parent"
                 android:layout_height="?android:attr/listPreferredItemHeight"
                 android:ellipsize="marquee"
                 android:textColor="#000000"/>

Then change your code to this:

ArrayAdapter mAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array. action_bar_spinner, android.R.layout.simple_spinner_dropdown_item);
        mAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        mode.setAdapter(mAdapter);


Have you tried putting the spinner in the xml file like this:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary">

    <Spinner
            android:id="@+id/spinner_nav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>

And also disable the title like this:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

Answer is from Chris Banes: http://stackoverflow.com/a/26511653/2767703

这篇关于更改微调风格的工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:09