Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

android - ActionBar setBackgroundDrawable() nulling background from Thread/Handler

I'm trying to change the background of the ActionBar from a Handler. The end goal is to pass the Handler to an AsyncTask, but for now even calling Handler.sendMessage() from a Thread results in strange behavior. Through the debugger I'm able to see that the Handler receives the Message and subsequently runs setActionBarBackground() through to the end.

The default ActionBar with blue bottom stroke fully disappears from the screen and is not replaced by the new GradientDrawable. I suspect that the background is being somehow nullified. Furthermore, when I focus on the EditText again, the correct GradientDrawable ActionBar background appears. The behavior I would expect is for the background to simple change on actionDone.

Any insight as to why this is happening would be greatly appreciated!

Relevant code:

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hendrik's workaround does the trick, but not when using a custom title view. This works instead:

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

You may need a title set for this to work though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...