如何在导航抽屉中进行自定义设计?

时间:2014-01-08 05:33:46

标签: android android-layout android-navigation

我在我的应用中使用Navigation Drawer。我想在导航抽屉中更改Tittle,我想在标题顶部设置后退按钮选项。

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

在Java Text之后我想添加一个后退按钮,以便我可以在我的应用程序中提供Back功能。

我的完整代码

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

3 个答案:

答案 0 :(得分:1)

在您的片段中,您可以

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

我猜你在谈论<

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

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

更改图标

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

我不知道改变标题颜色是什么意思。

选中“自定义样式”操作栏

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

同时检查此

Remove application icon and title from Honeycomb action bar

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

答案 1 :(得分:1)

在清单文件中,使用

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

并在styles.xml中添加以下代码

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

答案 2 :(得分:0)

将此功能设置为ActionBar可帮助您实现所需目标。 请记住,您需要创建自己的操作栏布局。

  

getSupportActionBar()setCustomView(R.layout.my_custom_actionbar_layout)。           getSupportActionBar()setDisplayShowTitleEnabled(假)。           getSupportActionBar()setDisplayShowCustomEnabled(真)。           getSupportActionBar()setDisplayUseLogoEnabled(假)。           getSupportActionBar()setDisplayHomeAsUpEnabled(假)。           getSupportActionBar()setDisplayShowHomeEnabled(假);