android - ActionBar Compat 在 Android 2.3.3 上不显示 Logo

标签 android android-actionbar

我正在尝试使用最新的 support-v7 库中提供的新 ActionBar Compat。 使用 SuppportV7Demos 中的示例,当我触摸“DISPLAY_USE_LOGO”时,图标没有改变。

因为我也不能让它在我自己的应用程序中工作,有人实现了吗?

ActionBarDisplayOptions.java

package com.example.android.supportv7.app;

public class ActionBarDisplayOptions extends ActionBarActivity
    implements View.OnClickListener, ActionBar.TabListener {
private View mCustomView;
private ActionBar.LayoutParams mCustomViewLayoutParams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.action_bar_display_options);

    findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
    findViewById(R.id.toggle_show_home).setOnClickListener(this);
    findViewById(R.id.toggle_use_logo).setOnClickListener(this);
    findViewById(R.id.toggle_show_title).setOnClickListener(this);
    findViewById(R.id.toggle_show_custom).setOnClickListener(this);
    findViewById(R.id.toggle_navigation).setOnClickListener(this);
    findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
    findViewById(R.id.toggle_visibility).setOnClickListener(this);

    // Configure several action bar elements that will be toggled by display options.
    mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
    mCustomViewLayoutParams = new ActionBar.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    final ActionBar bar = getSupportActionBar();
    bar.setCustomView(mCustomView, mCustomViewLayoutParams);
    bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
    bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));
    bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.display_options_actions, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    finish();
    return true;
}

@Override
public void onClick(View v) {
    final ActionBar bar = getSupportActionBar();
    int flags = 0;
    switch (v.getId()) {
        case R.id.toggle_home_as_up:
            flags = ActionBar.DISPLAY_HOME_AS_UP;
            break;
        case R.id.toggle_show_home:
            flags = ActionBar.DISPLAY_SHOW_HOME;
            break;
        case R.id.toggle_use_logo:
            flags = ActionBar.DISPLAY_USE_LOGO;
            break;
        case R.id.toggle_show_title:
            flags = ActionBar.DISPLAY_SHOW_TITLE;
            break;
        case R.id.toggle_show_custom:
            flags = ActionBar.DISPLAY_SHOW_CUSTOM;
            break;
        case R.id.toggle_navigation:
            bar.setNavigationMode(
                    bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD
                            ? ActionBar.NAVIGATION_MODE_TABS
                            : ActionBar.NAVIGATION_MODE_STANDARD);
            return;
        case R.id.cycle_custom_gravity: {
            ActionBar.LayoutParams lp = mCustomViewLayoutParams;
            int newGravity = 0;
            switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                case Gravity.LEFT:
                    newGravity = Gravity.CENTER_HORIZONTAL;
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    newGravity = Gravity.RIGHT;
                    break;
                case Gravity.RIGHT:
                    newGravity = Gravity.LEFT;
                    break;
            }
            lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
            bar.setCustomView(mCustomView, lp);
            return;
        }
        case R.id.toggle_visibility:
            if (bar.isShowing()) {
                bar.hide();
            } else {
                bar.show();
            }
            return;
    }

    int change = bar.getDisplayOptions() ^ flags;
    bar.setDisplayOptions(change, flags);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}

和: action_bar_display_options.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <Button android:id="@+id/toggle_home_as_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_home_as_up" />
    <Button android:id="@+id/toggle_show_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_show_home" />
    <Button android:id="@+id/toggle_use_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_use_logo" />
    <Button android:id="@+id/toggle_show_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_show_title" />
    <Button android:id="@+id/toggle_show_custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_show_custom" />
    <Button android:id="@+id/toggle_navigation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_navigation" />
    <Button android:id="@+id/cycle_custom_gravity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cycle_custom_gravity" />
    <Button android:id="@+id/toggle_visibility"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_visibility" />
</LinearLayout>
</ScrollView>

最佳答案

在我的应用程序的 actionbarsherlock 版本中,我在 list 中设置了图标和 Logo 。更改为 ActionBarCompat 后, Logo 未显示在 Android 2.2、2.3.x 上的操作栏中。

为了使 Logo 显示,我在代码中进行了设置

ActionBar ab = getSupportActionBar();
ab.setLogo(R.drawable.logo);

并从 list 中删除了 'android:logo="@drawable/logo_icon"'。

在 Android 2.2、2.3.3 和 4.1.2 上测试。

关于android - ActionBar Compat 在 Android 2.3.3 上不显示 Logo ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997824/

相关文章:

android - 如何在默认主题 Theme.Holo.Light 中更改 homeAsUpIndicator 的图像

android - 如何在调用 FragmentManager.replace() 时不调用 onCreateOptionsMenu

Java If/Else 语句决策

java - 从 onStopCommand 返回是否会结束 Android 服务?

java - 更改 fragment 中的 ActionBar 标题

android - 操作栏选项卡必须有回调

android - 无法将自定义 View 添加到 Android 操作栏,为什么?

java - Android 在谷歌支付中绘制自定义 View

java - 无法初始化 Parse SDK 测试

android - 调用 ActionBarDrawerToggle.setDrawerIndicatorEnabled(false) 后向上箭头不显示