11

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

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

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

相关代码:

测试活动.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

4 回答 4

18

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

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

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

于 2012-08-16T19:53:52.860 回答
4

我遇到了同样的问题。当我将操作栏的背景动态更改为setBackgroundDrawable()newGradientDrawable时,操作栏将变为全黑。

我发现这与操作栏标题有关。我不使用标题并将其设置为空字符串。出于测试目的,我将其设置为测试字符串“Test”,问题setBackgroundDrawable()立即消失。对我来说,这看起来像是 ICS 中的错误。

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

actionBar.setBackgroundDrawable(newBackground);
actionBar.setTitle(".");
actionBar.setTitle("");
于 2012-08-07T09:46:30.227 回答
0

好吧,这似乎是一个疯狂的边缘案例,它发生在 ActionBarSherlock 和 ActionBar 的 Android 实现中。我最终调用 ab.hide() 和 ab.show() 来解决它...:(

于 2012-06-19T14:51:27.253 回答
0

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

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

于 2012-07-11T19:49:07.657 回答