动作条两侧的标志为中心,实施项目 [英] ActionBar logo centered and Action items on sides

查看:111
本文介绍了动作条两侧的标志为中心,实施项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能做出动作条有图案的中心,两个行动项目就这样边?

How could I make an actionbar to have logo in the center and two action items on sides like this?

我会用动作条夏洛克。

推荐答案

正如我在评论说,我不是很确定如果有的话,这一切与ABS改变(我敢肯定,没有多少会),但与标准的操作栏,你可以加载自定义布局的.xml为你的动作条的标题。例如,你可以有action_bar_title.xml:

As I noted in a comment, I am not exactly sure if any of this would change with ABS (I'm sure not much would), but with the standard Action Bar you can load a custom layout .xml for your action bar title. For example, you could have action_bar_title.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/your_desired_background" >

<TextView
        android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

</RelativeLayout>

然后,在你的活动,你想打电话给这样的方法的onCreate():

Then, in your Activity, you would want to call a method like this in onCreate():

private void setupActionBar() {
        ActionBar ab = getActionBar();
        ab.setDisplayShowCustomEnabled(true);
        ab.setDisplayShowTitleEnabled(false);
        ab.setIcon(R.drawable.your_left_action_icon);
        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.action_bar_title, null);

        TextView titleTV = (TextView) v.findViewById(R.id.title);
        Typeface font = Typeface.createFromAsset(getAssets(),
                "fonts/your_custom_font.ttf");
        titleTV.setTypeface(font);

        ab.setCustomView(v);

            ab.setHomeAsUpEnabled(true);
    }

要采取左操作项目的控制权,你可以在你的活动做到这一点:

To take control of the left action item, you could do this in your Activity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.yourRightActionItem:
                        //do something
                        return true;
        case android.R.id.home: //<-- this will be your left action item
            // do something
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

当然,要确保你也做了其他必要的设置的操作项在菜单XML的为好。这将是任何权利单方面行动项目。

Of course, make sure you also do the other necessary setups for the Action items in your menu xml's as well. This will be for any right-sided action items.

编辑:就在你说的那个标题将是一个形象的另一个注释看见。如果是这样的情况下,只需更换我用我的例子与您的ImageView TextView的,而忽略了setupActionBar的字体自定义code()方法。

Just saw in another comment you said that title would be an image. If that's the case, just replace the TextView I used in my example with your ImageView, and disregard the font customization code in the setupActionBar() method.

EDIT2:我更新了我的Java code。你需要做的ab.setHomeAsUpEnabled(真),然后有一个自定义主题,以实现一种无形的(或者@null虽然我不知道是否Android将接受)绘制的回家了指标。看到这里(摘自答案<一个href="http://stackoverflow.com/questions/9252354/how-to-customize-the-back-button-on-actionbar">here:)

I updated my Java code. You will need to do ab.setHomeAsUpEnabled(true), and then have a custom theme to implement an invisible (or perhaps @null although I don't know if Android will accept that) drawable for the home as up indicator. See here (taken from answer here:)

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
    <item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
    <!-- or you could try @null -->
</style>

这篇关于动作条两侧的标志为中心,实施项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆