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

时间:2012-06-12 18:34:08

标签: android handler android-actionbar actionbarsherlock

我正在尝试从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);
    }

}

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>

4 个答案:

答案 0 :(得分:18)

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

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

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

答案 1 :(得分:4)

我遇到了同样的问题。当我使用setBackgroundDrawable()动态更改操作栏的背景为新GradientDrawable时,操作栏会全黑。

我发现它已连接到操作栏标题。我不使用标题并将其设置为空字符串。出于测试目的,我将其设置为测试字符串“Test”,而setBackgroundDrawable()的问题立即消失。看起来像是ICS中的一个错误。

因此,不是隐藏和显示操作栏,而是以下解决方法适用于我:

actionBar.setBackgroundDrawable(newBackground);
actionBar.setTitle(".");
actionBar.setTitle("");

答案 2 :(得分:0)

这看起来像一个疯狂的边缘情况,它发生在ActionBarSherlock和ActionBar的Android实现中。我最后调用了ab.hide()和ab.show()来解决它...... :(

答案 3 :(得分:0)

我也在使用ActionBarSherlock,我也想在任意时间通过调用Drawable来更改ActionBar的后台setBackgroundDrawable()。但是,我发现在背景颜色设置一次之后,对setBackgroundDrawable()的后续调用似乎没有任何效果。我认为这与您遇到的问题类似。

为了解决这个问题,我作弊了。我只是在我的布局中放置一个View,它出现在ActionBar后面,并使ActionBar本身透明。通过调用ActionBar上的View,可以轻松地将getHeight()的高度设置为正确的高度。

相关问题