gpt4 book ai didi

android - ActionBar setBackgroundDrawable() 从线程/处理程序中取消背景

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:13 26 4
gpt4 key购买 nike

我正在尝试通过处理程序更改 ActionBar 的背景。最终目标是将 Handler 传递给 AsyncTask,但现在即使从 Thread 调用 Handler.sendMessage() 也会导致奇怪的行为。通过调试器,我能够看到 Handler 收到消息并随后运行 setActionBarBackground() 直到结束。

带有蓝色底部描边的默认 ActionBar 完全从屏幕上消失,并且没有被新的 GradientDrawable 取代。我怀疑背景以某种方式被取消了。此外,当我再次关注 EditText 时,会出现正确的 GradientDrawable ActionBar 背景。我期望的行为是背景对 actionDone 的简单更改。

任何有关为什么会发生这种情况的见解都将不胜感激!

相关代码:

测试 Activity .java

public class TestActivity extends RoboSherlockFragmentActivity {

@InjectView(R.id.ET_test) EditText testET;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(MainApp.TAG, "onCreate");
setContentView(R.layout.test_activity);

testET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
String loc = testET.getText().toString();
InputMethodManager mgr = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(testET.getWindowToken(), 0);
Toast.makeText(TestActivity.this, "EditText done!", Toast.LENGTH_SHORT).show();

/*TestQuery tq = new TestQuery(TestActivity.this, mHandler);
tq.execute();*/
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.sendMessage(new Message());
}
}).start();
}
return true;
}
});
}

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//setActivityColors();
setActionBarBackground();
}
};

private void setActionBarBackground() {
ActionBar ab = getSupportActionBar();
//Drawable d = WidgetUtils.getActionBarDrawable(TestActivity.this, 0xFF00FFFF);

GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{0xFFFFFFFF, 0xFF000000});
gd.setCornerRadius(0f);
ab.setBackgroundDrawable(gd);
}

}

测试 Activity .xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button"/>
<EditText
android:id="@+id/ET_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:maxLines="1"
android:lines="1"
android:inputType="number"
android:imeOptions="actionDone"
android:nextFocusUp="@id/ET_test"
android:nextFocusLeft="@id/ET_test"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button2"/>

</LinearLayout>

最佳答案

Hendrik 的解决方法可以解决问题,但在使用自定义标题 View 时却不行。这反而有效:

actionBar.setBackgroundDrawable(newBackground);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

不过,您可能需要一个标题集才能正常工作。

关于android - ActionBar setBackgroundDrawable() 从线程/处理程序中取消背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002691/

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