准备自定义操作栏

时间:2013-05-27 06:10:48

标签: android actionbarsherlock customization

我想要完全像下面的图像。

enter image description here

但我现在能够得到下面的一个。我有两个问题。

1)如何让actionbar处理所有活动而不是我现在正在充气的单项活动。

2)上面的图片看起来像是高架的布局栏,但我看起来像普通的面板。有人可以帮助我如何实现这些目标吗?代码片段表示赞赏。

我目前的输出:

enter image description here

要扩展到操作栏的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:background="@drawable/ab_customshape" >

    <ImageButton
            android:id="@+id/homeicon"
            android:layout_width="40sp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" >
     </ImageButton>

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="My Title"
        android:layout_centerVertical="true"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold" />
    <ImageButton 
        android:id="@+id/rightbutton"
            android:layout_width="40sp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher"
            android:background="@android:color/transparent"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true" 
        />

</RelativeLayout>

活动代码段:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initActionBar();
    }

    private void initActionBar() {
        mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        mInflater = LayoutInflater.from(this);
        mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText("My Own Title");
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
    }

@绘制/ ab_customshape:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="-90"
        android:endColor="#DD2ECCFA"
        android:startColor="#DD000000"
        android:type="linear" />

</shape>

1 个答案:

答案 0 :(得分:1)

1)如何让这个操作栏对所有活动起作用,而不是我现在正在膨胀的单一活动。

将以下代码添加到style.xml。

<style name="Theme.Styled" parent="Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="android:actionModeCloseDrawable">@drawable/collapse_button</item>
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="background">@drawable/ab_customshape</item>
    <item name="android:background">@drawable/ab_customshape</item>

</style>

然后将此设置为您的清单中的应用主题,例如

<application
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.Styled" > //Here we set the theme

并尝试向您的操作栏添加按钮,

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    menu.add(0, HOMEBUTTON, 0, "Home").setIcon(R.drawable.home_button)

    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return true;
}

enter image description here

和9补丁enter image description here

相关问题