Preparing custom action bar

送分小仙女□ 提交于 2019-12-10 18:35:06

问题


I am trying to get exactly like the below image.

But I am able to get the below one right now. Here I have two questions.

1) How to make this actionbar to be worked on all activities instead of single activity that I am inflating now.

2) The above image seems like elevated layout bar, but mine looks like a normal panel. Can some one please help me how to achieve these? Code snippets are appreciated.

My current output:

My layout code to inflate to actionbar:

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

Activity code snippets:

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

@drawable/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:


1) How to make this actionbar to be worked on all activities instead of single activity that I am inflating now.

Add below code to your 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>

Then set this as your app theme in your manifest like,

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

And try to add buttons to your actionbar like this,

@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;
}

and 9 patch here



来源:https://stackoverflow.com/questions/16767259/preparing-custom-action-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!