gpt4 book ai didi

android - 以编程方式设置微调器文本颜色滞后,速度慢,瞬间颜色错误

转载 作者:太空狗 更新时间:2023-10-29 16:33:34 29 4
gpt4 key购买 nike

TLDR:我的微调器瞬间显示了错误的颜色。

我的微调器有问题。每当我运行应用程序时,如果 Activity 没有缓存在内存中,它有时会滞后。在我可以将其设置为正确的颜色之前,文本是默认颜色(如黑色)。看起来真的很不专业。

视频:请观看此屏幕录像以了解实际效果: https://drive.google.com/file/d/0By2AG5yaBEhMRnRsbVBDU251STQ/view

它在加载页面时如何寻找一瞬间: enter image description here

延迟时间过后它的样子(以及它从一开始应该看起来的样子): enter image description here

代码:

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:

<?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>

最佳答案

我做错了什么:

之前,我听从了 this answer 的建议,并在 onItemSelected 方法中设置文本颜色,但该方法仅在 UI 完成后自动调用,您不能直接从代码中调用 onItemSelected。那导致了滞后。 (但是当您从下拉列表中选择一个项目时仍然需要它 - 请参阅我对这个问题的解决方案。)

解决方案:

策略是在 onCreate 完成之前获取“Selected” View 并设置其文本颜色。当我在调试器中对其进行测试时,onCreate 方法期间没有显示任何 UI,因此可以保证它可以正常工作。

我只需要在调用 setAdapter(...) 之后添加这段代码:

//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);

关键点是调用 spinner.setSelection(0, true) 使用 true 参数。否则,如果您只调用 spinner.setSelection(0), View v 将为空。感谢this answer,我发现了这一点.

完整方法:

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

@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)
{
}
});

}

有关 setSelection 方法源代码的更多信息,请参阅此处的 AbsSpinner.java 代码:https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/AbsSpinner.java

这里是 Spinner.java 以防它有帮助:https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/Spinner.java

关于android - 以编程方式设置微调器文本颜色滞后,速度慢,瞬间颜色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908564/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com