android - 使用 setTitle 后操作栏搞砸了

标签 android android-actionbar

我正在尝试在 Activity 中设置 actionBar 的标题。我在一项 Activity 中有 3 个布局 View 。每个 View 布局显示支付过程的不同状态。现在我设置了标题,在某个阶段操作栏背景变得困惑。我想知道为什么。

如果我注释掉以下行,则不会发生这种情况。

onClick() {
....
    getActionBar().setTitle("Customer Payment");
....

在 Activity 的 onCreate 中,我运行以下命令来设置操作栏背景。

private void setupActionBar() {
    Drawable backgroundColor;
    switch (getIntent().getIntExtra(God.HOME_SCREEN_OPERATION,
            God.INVALID_ID)) {
    case God.OPERATION_RECHARGE:
        getActionBar().setIcon(R.drawable.icon_mobile);
        backgroundColor = new ColorDrawable(getResources().getColor(
                R.color.RechargeBackgroundColor));
        getActionBar().setBackgroundDrawable(backgroundColor);
        mobileServiceSummary.setVisibility(View.VISIBLE);
        serviceInfoLayout.setBackground(backgroundColor);
        serviceInfoIcon.setImageResource(R.drawable.icon_mobile);
        break;
    case God.OPERATION_FACILITY:
        getActionBar().setIcon(R.drawable.icon_facility);
        backgroundColor = new ColorDrawable(getResources().getColor(
                R.color.ToiletBackgroundColor));
        getActionBar().setBackgroundDrawable(backgroundColor);
        facilityServiceSummary.setVisibility(View.VISIBLE);
        serviceInfoLayout.setBackground(backgroundColor);
        serviceInfoIcon.setImageResource(R.drawable.icon_facility);
        break;
    case God.OPERATION_DTH:
        getActionBar().setIcon(R.drawable.icon_dth);
        backgroundColor = new ColorDrawable(getResources().getColor(
                R.color.DthBackgroundColor));
        getActionBar().setBackgroundDrawable(backgroundColor);
        dthServiceSummary.setVisibility(View.VISIBLE);
        serviceInfoLayout.setBackground(backgroundColor);
        serviceInfoIcon.setImageResource(R.drawable.icon_dth);
        break;
    // case R.id.mseb_payment:
    // getActionBar().setIcon(R.drawable.icon_mseb);
    // msebServiceSummary.setVisibility(View.VISIBLE) ;
    // break;
    default:
        break;
    }
}

一些更多的代码..

private void enableCustomerPayment() {
        getActionBar().setTitle("Customer Payment");
        getActionBar().setSubtitle(
                "Pincode of customer needed for payment permission.");
        getActionBar().setDisplayHomeAsUpEnabled(false);
        getActionBar().setHomeButtonEnabled(false);
        getActionBar().setDisplayShowCustomEnabled(false) ;
        getActionBar().setDisplayUseLogoEnabled(false) ;

        findViewById(R.id.next_button).setVisibility(View.GONE);
        findViewById(R.id.payment_button).setVisibility(View.VISIBLE);
        findViewById(R.id.done_button).setVisibility(View.GONE);

        operatorLockLayout.setVisibility(View.GONE);
        customerLoginAndConfirmationLayout.setVisibility(View.VISIBLE);
        customerPaymentLayout.setVisibility(View.GONE);
        customerConfirmLayout.setVisibility(View.VISIBLE);
        // customerConfirmSpaceLayout.setVisibility(View.VISIBLE);
    }

    private void enablePaymentConfirmation() {
        getActionBar().setTitle("Payment Confirmation");
        getActionBar().setSubtitle("Thankyou for your payment.");
        setupActionBar();
        getActionBar().setDisplayHomeAsUpEnabled(false);
        getActionBar().setHomeButtonEnabled(false);

        findViewById(R.id.next_button).setVisibility(View.GONE);
        findViewById(R.id.payment_button).setVisibility(View.GONE);
        findViewById(R.id.done_button).setVisibility(View.VISIBLE);

        operatorLockLayout.setVisibility(View.GONE);
        customerLoginAndConfirmationLayout.setVisibility(View.VISIBLE);
        customerPaymentLayout.setVisibility(View.VISIBLE);
        customerConfirmLayout.setVisibility(View.GONE);
        // customerConfirmSpaceLayout.setVisibility(View.GONE);
    }

在 styles.xml 中,颜色的设置如下。而且颜色效果很好。

<color name="NewWalletBackgroundColor">#FFD54E</color>
<color name="BalanceBackgroundColor">#FFD54E</color>
<color name="DepositBackgroundColor">#FFD54E</color>
<color name="MsebBackgroundColor">#E57272</color>
<color name="RechargeBackgroundColor">#81C784</color>
<color name="DthBackgroundColor">#AB6BAC</color>
<color name="ToiletBackgroundColor">#56C0ED</color>

操作栏乱七八糟

enter image description here

在这里,Action Bar 背景是完全蓝色的。这是我所期望的。

enter image description here

编辑

Seems like there is a issue with the height, it starts up with 96 and when its messed up the height is 0.

我现在该如何解决这个问题?

最佳答案

在我看来,默认操作栏需要在操作栏右侧有一个抽屉导航图标图标。我认为您所说的空间上有一个透明按钮。无论您是否为此 Activity 设置抽屉,如果您使用默认方法设置操作栏,它都会在那里。首先尝试这些步骤

  setDrawerIndicatorEnabled(false).
  actionBar.setDisplayHomeAsUpEnabled(false) 
  actionBar.setHomeButtonEnabled(false); 

如果这不起作用,请使用自定义操作栏。

做这样的事情。为什么不使用默认操作栏而不是尝试设置自定义 View (纯 TextView,其宽度设置为与父级匹配)。

       this.getActionBar().setDisplayShowCustomEnabled(true);
       this.getActionBar().setDisplayShowTitleEnabled(false);

       LayoutInflater inflator = LayoutInflater.from(this);
       View v = inflator.inflate(R.layout.titleview, null);

      //if you need to customize anything else about the text, do it here.
      ((TextView)v.findViewById(R.id.title)).setText(this.getTitle());

      //assign the view to the actionbar
      this.getActionBar().setCustomView(v);

关于android - 使用 setTitle 后操作栏搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31624208/

相关文章:

android:从子菜单中删除一个项目

android - 在 ActionBar-PullToRefresh 库中更改加载屏幕的文本

android - 为什么 Observable.interval() 不能取消订阅?

没有 Activity 的Android Context?和其他无 Activity 编程?

android - 首次打开应用程序时如何仅启动一次 Activity ?

Android 查看分页器和列表 fragment

javascript - 使用 Javascript 检测操作系统类型并根据操作系统执行不同的操作

java - 单击其他歌曲时如何暂停和重置所有正在播放的歌曲?

android - 为什么 Android 会截断我的 ActionBar 标题?

android - 单击按钮更改操作栏标题