ActionBar setBackgroundDrawable()使用Thread/Handler归零背景

Roc*_*off 11 android handler actionbarsherlock android-actionbar

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

具有蓝色底部笔划的默认ActionBar完全从屏幕上消失,并且不会被新的GradientDrawable替换.我怀疑背景在某种程度上是无效的.此外,当我再次关注EditText时,会出现正确的GradientDrawable ActionBar背景.我期望的行为是对actionDone进行简单更改的背景.

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

相关代码:

TestActivity.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);
    }

}
Run Code Online (Sandbox Code Playgroud)

test_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>
Run Code Online (Sandbox Code Playgroud)

Mat*_*ino 18

Hendrik的解决方法可以解决问题,但在使用自定义标题视图时则不行.这适用于:

actionBar.setBackgroundDrawable(newBackground);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
Run Code Online (Sandbox Code Playgroud)

您可能需要一个标题集才能使用.