【问题标题】:Action Bar messed up after using setTitle使用 setTitle 后操作栏搞砸了
【发布时间】:2015-08-15 06:28:00
【问题描述】:

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

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

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

在活动的 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>

操作栏搞砸了

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

编辑

似乎高度有问题,它以96开始,当它搞砸时高度为0。

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

【问题讨论】:

  • 操作栏的更多代码
  • 你想要的预期输出是什么?能否请您添加最终图像。这样我就可以了解一下,然后我将添加我之前使用的自定义代码。
  • 我希望整个操作栏都有背景颜色。我想知道为什么会有这个差距。
  • 在另一个视图中有你想要的蓝色操作栏吗?
  • 是的..我需要它全蓝色..

标签: android android-actionbar


【解决方案1】:

在我看来,默认的操作栏需要一个用于操作栏右侧的导航抽屉图标的图标。我认为您正在谈论的空间上有一个透明按钮。即使您为此活动设置了抽屉,如果您使用默认方法设置操作栏,它也会在那里。首先尝试这些步骤

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

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

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

       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);

【讨论】:

  • 找不到方法setDrawerIndicatorEnabled(false)。
  • 所以你没有在这里使用导航抽屉,对吧?因为该方法适用于drawerToggle 类。你试过第二种方法吗?
【解决方案2】:

我从您的问题中假设 enablePaymentConfirmation 正在使用 getActionBar().setTitle("Customer Payment"); 存在的代码,并且当它被注释掉时。如果它也在发挥作用,那么我将需要改变我的答案。

看不到您的 xml 会导致更多猜测工作,但据我所知,我会建议以下内容。
1. 在您的活动中向您的OnCreate 致电setupActionBar()。这样,它就在您的活动期间进行设置。如果在 enablePaymentConfirmation() 之前调用 enableCustomerPayment(),则在 OnCreate 中不调用它,操作栏设置将不会被调用。

2。 除非您未显示以下代码更改,否则这些可以进入您的 setupActionbar:

 getActionBar().setDisplayHomeAsUpEnabled(false);
 getActionBar().setHomeButtonEnabled(false);

3。 以下是您展示的两种方法之间对 ActionBar 的两个更改:

getActionBar().setDisplayShowCustomEnabled(false) ;
getActionBar().setDisplayUseLogoEnabled(false) ;

4。 我不确定你为什么只在enableCustomerPayment() 中设置setDisplayShowCustomEnabled。我看不到您正在使用 customView。 - 如果不是,也可以进入您的设置。 - 否则,您将需要在所有方法中调用它并进行更改。 - 如果您在某些地方而不是其他地方为您的操作栏使用自定义视图,这将影响它的显示方式。特别是如果您不管理 bool 更改。

5。 徽标也是如此,我看不到您在哪里使用它。所以我会把它放在设置中。如果您将它用于其他人并且您的 xml 设置不正确,这可能是背景颜色不同的原因。

希望这会有所帮助。随意发布更多信息,xml会很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多