【问题标题】:ActionBar setBackgroundDrawable() nulling background from Thread/HandlerActionBar setBackgroundDrawable() 从 Thread/Handler 清空背景
【发布时间】:2012-06-12 18:34:07
【问题描述】:

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

【问题讨论】:

    标签: android handler android-actionbar actionbarsherlock


    【解决方案1】:

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

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

    你可能需要一个标题集才能工作。

    【讨论】:

    • 感谢@Mat 为我工作,但如果您需要 Title,您可以更改启用标题的状态 :)
    • 如果您不知道标题是否显示,则此方法有效: boolean isDisplayingTitle = (actionBar.getDisplayOptions() & ActionBar.DISPLAY_SHOW_TITLE) != 0; actionBar.setDisplayShowTitleEnabled(!isDisplayingTitle); actionBar.setDisplayShowTitleEnabled(isDisplayingTitle);
    • 我遇到了同样的问题。我添加了 bar.setBackgroundDrawable(newBackground);在 onCreate() 方法中,但后来将线条移动到 onPostCreate() 它对方向变化起作用。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 这个解决方法对我不起作用:( ...只有隐藏/显示在可能的情况下有效
    【解决方案3】:

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

    【讨论】:

    • 这行得通。一个问题。你知道如何防止屏幕其余部分的上升和下降吗?
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      相关资源
      最近更新 更多