11

ActionBarの背景をハンドラーから変更しようとしています。最終的な目標は、ハンドラーをAsyncTaskに渡すことですが、今のところ、スレッドからHandler.sendMessage()を呼び出すと、奇妙な動作が発生します。デバッガーを介して、ハンドラーがメッセージを受信し、その後setActionBarBackground()を最後まで実行していることがわかります。

下のストロークが青いデフォルトのActionBarは画面から完全に消え、新しいGradientDrawableに置き換えられません。背景がどういうわけか無効になっているのではないかと思います。さらに、EditTextに再度フォーカスすると、正しいGradientDrawableActionBarの背景が表示されます。私が期待する動作は、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

4 に答える 4

18

Hendrik の回避策はこのトリックを実行しますが、カスタム タイトル ビューを使用する場合はそうではありません。これは代わりに機能します:

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

ただし、これを機能させるには、タイトル セットが必要になる場合があります。

于 2012-08-16T19:53:52.860 に答える
4

私は同じ問題に遭遇しました。setBackgroundDrawable()アクションバーの背景を新しいに動的に変更するとGradientDrawable、アクションバーがすべて黒になります。

これがアクションバーのタイトルに関連していることを発見しました。タイトルを使用せず、空の文字列に設定しました。テストの目的で、代わりにテスト文字列「Test」に設定しましたが、問題はsetBackgroundDrawable()すぐに解消されました。私にはICSのバグのように見えます。

したがって、アクションバーを非表示にして表示する代わりに、次の回避策が有効です。

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

私も ActionBarSherlock を使用していますが、任意の時点でDrawableへの呼び出しを介して ActionBarの背景を変更したいと考えていました。setBackgroundDrawable()ただし、背景色が一度設定されると、その後の への呼び出しはsetBackgroundDrawable()効果がないように見えることがわかりました。これはあなたが遭遇した問題に似ていると思います。

それを回避するために、私はだまされました。ActionBar の背後に表示される をレイアウトに配置Viewし、ActionBar 自体を透明にしました。の高さは、ActionBarViewを呼び出して正しい高さに簡単に設定できます。getHeight()

于 2012-07-11T19:49:07.657 に答える
0

これはクレイジーなエッジ ケースのように思えます。これは、ActionBarSherlock と ActionBar の Android 実装の両方で発生します。私はそれを回避するために ab.hide() と ab.show() を呼び出すことになりました...:(

于 2012-06-19T14:51:27.253 に答える