Android 以编程方式设置微调器文本颜色滞后、速度慢,是错误的分秒颜色

Android 以编程方式设置微调器文本颜色滞后、速度慢,是错误的分秒颜色,android,spinner,Android,Spinner,TLDR:我的微调器在一瞬间显示错误的颜色 我的旋转器有问题。每当我运行应用程序时,如果活动未缓存在内存中,它有时会延迟。文本是默认颜色(如黑色),然后我才能将其设置为正确的颜色。看起来真的很不专业 视频: 请观看此屏幕录制以查看此操作: 加载页面时一瞬间的外观: 滞后时间后的外观(以及从一开始的外观): 代码: public class MyActivity extends AppCompatActivity { @Override protected void onC

TLDR:我的微调器在一瞬间显示错误的颜色

我的旋转器有问题。每当我运行应用程序时,如果活动未缓存在内存中,它有时会延迟。文本是默认颜色(如黑色),然后我才能将其设置为正确的颜色。看起来真的很不专业

视频: 请观看此屏幕录制以查看此操作:

加载页面时一瞬间的外观:

滞后时间后的外观(以及从一开始的外观):

代码:

public class MyActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        //Get rid of the normal toolbar's title, because the spinner is replacing the title.
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        //Set the choices on the spinner by setting the adapter.
        spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

        //Set the listener for when each option is clicked.
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                //Change the selected item's text color
                ((TextView) view).setTextColor(backgroundColor);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
                                   android:background="@color/ColorPrimary"
                                   android:elevation="4dp">
    <Spinner
        android:id="@+id/spinner"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
公共类MyActivity扩展了AppCompatActivity
{
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
微调器微调器=(微调器)findViewById(R.id.Spinner);
//去掉普通工具栏的标题,因为微调器正在替换标题。
getSupportActionBar().setDisplayShowTitleEnabled(false);
//通过设置适配器在微调器上设置选项。
setAdapter(新的SpinnerAdapter(toolbar.getContext(),新字符串[]{“概述”、“故事”、“规范”、“轮询”、“视频”}、accentColor、backgroundColor));
//设置单击每个选项时的侦听器。
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener()
{
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id)
{
//更改选定项目的文本颜色
((TextView)视图).setTextColor(backgroundColor);
}
@凌驾
未选择公共无效(AdapterView父级)
{
}
});
}
}
XML:

public class MyActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        //Get rid of the normal toolbar's title, because the spinner is replacing the title.
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        //Set the choices on the spinner by setting the adapter.
        spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

        //Set the listener for when each option is clicked.
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                //Change the selected item's text color
                ((TextView) view).setTextColor(backgroundColor);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
                                   android:background="@color/ColorPrimary"
                                   android:elevation="4dp">
    <Spinner
        android:id="@+id/spinner"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>

我做错了什么:

public class MyActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        //Get rid of the normal toolbar's title, because the spinner is replacing the title.
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        //Set the choices on the spinner by setting the adapter.
        spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

        //Set the listener for when each option is clicked.
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                //Change the selected item's text color
                ((TextView) view).setTextColor(backgroundColor);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
                                   android:background="@color/ColorPrimary"
                                   android:elevation="4dp">
    <Spinner
        android:id="@+id/spinner"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
以前,我遵循的建议,并在
onItemSelected
方法中设置文本颜色,但该方法仅在UI完成后自动调用,您不能直接从代码中调用
onItemSelected
。这造成了滞后。(但当您从下拉列表中选择一个项目时,仍然需要它-请参阅我对该问题的解决方案。)

解决方案:

public class MyActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        //Get rid of the normal toolbar's title, because the spinner is replacing the title.
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        //Set the choices on the spinner by setting the adapter.
        spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

        //Set the listener for when each option is clicked.
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                //Change the selected item's text color
                ((TextView) view).setTextColor(backgroundColor);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
                                   android:background="@color/ColorPrimary"
                                   android:elevation="4dp">
    <Spinner
        android:id="@+id/spinner"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
策略是在onCreate完成之前获取“Selected”视图并设置其文本颜色。当我在调试器中测试它时,在
onCreate
方法期间没有显示UI,因此这保证可以工作

我只需在调用
setAdapter(…)
后添加此代码:

关键点是使用
true
参数调用
spinner.setSelection(0,true)
。否则,如果只调用
spinner.setSelection(0)
,视图
v
将为空。多亏了你,我才知道这件事

完成方法:

下面是完整的方法注意:选中的
中的代码仍然需要在那里!因为否则,每次从下拉列表中选择一个项目时,它都会有错误的颜色

@Override 
protected void onCreate(Bundle savedInstanceState)
{ 
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    //Get rid of the normal toolbar's title, because the spinner is replacing the title. 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    //Set the choices on the spinner by setting the adapter. 
    spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

    //Set the text color of the Spinner's selected view (not a drop down list view)
    spinner.setSelection(0, true);
    View v = spinner.getSelectedView();
    ((TextView)v).setTextColor(backgroundColor);

    //Set the listener for when each option is clicked. 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    { 

        @Override 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        { 
           //Change the selected item's text color 
           ((TextView) view).setTextColor(backgroundColor);
        } 

        @Override 
        public void onNothingSelected(AdapterView<?> parent)
        { 
        } 
    }); 

} 
@覆盖
创建时受保护的void(Bundle savedInstanceState)
{ 
微调器微调器=(微调器)findViewById(R.id.Spinner);
//去掉普通工具栏的标题,因为微调器正在替换标题。
getSupportActionBar().setDisplayShowTitleEnabled(false);
//通过设置适配器在微调器上设置选项。
setAdapter(新的SpinnerAdapter(toolbar.getContext(),新字符串[]{“概述”、“故事”、“规范”、“轮询”、“视频”}、accentColor、backgroundColor));
//设置微调器选定视图(不是下拉列表视图)的文本颜色
微调器选择(0,真);
视图v=spinner.getSelectedView();
(文本视图)v.setTextColor(背景色);
//设置单击每个选项时的侦听器。
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener()
{ 
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id)
{ 
//更改选定项目的文本颜色
((TextView)视图).setTextColor(backgroundColor);
} 
@凌驾
未选择公共无效(AdapterView父级)
{ 
} 
}); 
} 
有关setSelection方法源代码的更多信息,请参阅此处的AbsSpinner.java代码:

下面是Spinner.java,以防它有所帮助: