سؤال

I am using Navigation Drawer in my app. I want to change the Tittle in the Navigation Drawer and I want to set the Back button option on the top of the header.

Three Thing i want to change in the header.
1.Remove the ic_launcher image
2.custom background theme 
3.Add a back button on the header

Image Link

After the Java Text I want add a back button so that I can provide a functionality of Back in my app.

My Complete Code

public class MainActivity extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private String mTitle = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = "Subjects";
        getActionBar().setTitle(mTitle);


        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.drawer_list);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.white_bar, R.string.drawer_open,
                R.string.drawer_close) {

            /** Called when drawer is closed */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu();
            }
        };

        // Setting DrawerToggle on DrawerLayout
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        if (savedInstanceState == null) {
            getUrl(0);
        }
        // Creating an ArrayAdapter to add items to the listview mDrawerList
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.drawer_list_item, getResources()
                        .getStringArray(R.array.menus));

        // Setting the adapter on mDrawerList
        mDrawerList.setAdapter(adapter);

        // Enabling Home button
        getActionBar().setHomeButtonEnabled(true);

        // Enabling Up navigation
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // Setting item click listener for the listview mDrawerList
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // Getting an array of rivers
                String[] menuItems = getResources().getStringArray(
                        R.array.menus);

                // Currently selected river
                mTitle = menuItems[position];
                getUrl(position);

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawerList);
            }
        });
    }

    @SuppressLint("Recycle")
    protected void getUrl(int position) {

        switch (position) {
        case 0:

        case 1:

        case 2:
        default:
            // return "";
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /** 
     * Called whenever we call invalidateOptionsMenu()
     *  */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
هل كانت مفيدة؟

المحلول

In your fragment you can do

getActivity().getActionBar().setTitle("My Title");

I guess you are talking about <.

http://developer.android.com/training/implementing-navigation/ancestral.html

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

To change icon

getActivity().getActionBar().setIcon(R.drawable.myappicon);

I don't know what you mean by changing the header color.

Check this for Custom Styling the action bar

https://developer.android.com/training/basics/actionbar/styling.html

Also check this

Remove application icon and title from Honeycomb action bar

getActivity().getActionBar.setIcon(android.R.color.transparent);

نصائح أخرى

In the manifest file change your code with

<application
        android:allowBackup="true"
        android:icon="@drawable/notes"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

and in the styles.xml add the below code

 <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowBackground">#FFE5E5E5</item>
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    </style>

    <style name="ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:icon">@drawable/notes</item>
        <item name="android:background">#FF666666</item>
    </style>

Setting this functions to ActionBar wiil help you to achieve what you want. Remember you need to create your own action bar layout.

getSupportActionBar().setCustomView(R.layout.my_custom_actionbar_layout); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setDisplayUseLogoEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(false); getSupportActionBar().setDisplayShowHomeEnabled(false);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top