简体   繁体   中英

Android setting custom actionbar

In my project i would to set a custom actionBar (with a custom layout) after a specific action. I have this simple Activity:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity2 extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            //apriImpostazioni ();
            return true;
        case R.id.information_item:
            //apriInformazioni ();
            return true;
        case R.id.search_item:
            apriBarraRicerca ();
            System.out.println ("IL BOTTONE RICERCA E' PREMUTO");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void apriBarraRicerca () {
    getActionBar ().setCustomView(getLayoutInflater().inflate(R.layout.search_layout, null));
}

}

Where "menu_main_activity2" is this xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="zipangulu.myapplication.MainActivity2">
<item android:id="@+id/action_settings"    android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/information_item"
    android:title="Info"
    android:icon="@drawable/info"
    android:orderInCategory="100"
    app:showAsAction="always"/>
<item android:id="@+id/search_item"
    android:title="Search"
    android:icon="@drawable/search_icon"
    android:orderInCategory="100"
    app:showAsAction="always"/>
</menu>

I would, pressing the search_item on the main actionbar, set a custom actionBar whith this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#4584d3">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/back_button"
    android:background="@null"
    android:src="@drawable/back_icon"/>

<AutoCompleteTextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="6"
    android:id="@+id/campo_ricerca"/>

<Spinner
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:spinnerMode="dropdown"
    android:id="@+id/spinner_anno"/>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@drawable/search_icon"
    android:id="@+id/avvia_ricerca"/>

</LinearLayout>

But at runtime I have a NullPointerException in the body of "apriBarraRicerca" method..why?How can I fix this problem?

EDIT: as suggested I replaced "getActionBar ()" with "getSupportActionBar ()", now I don't have any exception but nothing happens.

EDIT2: I added getSupportActionBar().setDisplayShowCustomEnabled(true); and now my custom actionbar is visible but not as i want, look at the following image: http://bit.ly/1Dc2kGg The bar is visible but cutted, and are visible also the items of the previous actionBar..

You can try do the same thing with the android.support.v7.widget.ToolBar

Your Layout which you can populate as you desire:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#688A08"
        android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>

You have to modify the values->styles.xml:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>
</resources>

And the in the onCreate() method you do the following:

Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
if(tb != null){
    setSupportActionBar(tb);
}

Hope it helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM