如何将图像添加到Drawer视图的底部?

时间:2014-02-11 18:46:38

标签: java android xml navigation-drawer

因此,我希望能够在与列表分开的抽屉视图底部放置徽标图像。我已经看到人们用其他列表视图和类似的东西做类似的事情,但每当我尝试实现时,我会得到很多java错误。任何有关这方面的帮助将不胜感激。

这是我的布局xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#f4f8f9" />
</android.support.v4.widget.DrawerLayout>

我已经在一些地方看到将帧布局更改为相对布局,但每次我这样做都会出错。

这是我的java代码:

public class someActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private String[] mDrawerItems;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerItems = getResources().getStringArray(R.array.drawer_items);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerList.setAdapter(new ShelfArrayAdapter(this, mDrawerItems)); 
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setSubtitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setSubtitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

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

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mDrawerItems[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

        // this changes fragments. 
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

3 个答案:

答案 0 :(得分:3)

在FrameLayout(子)中包含RelativeLayout。然后你可以把你的标志放在底部!希望有用!

修改

对不起,我觉得这很简单,但我确实有效!

试试这个:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

    <RelativeLayout
        android:id="@+id/ratafeia"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="start" >

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="240dip"
            android:layout_alignParentBottom="true"
            android:src="@drawable/rata" />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

和Java代码: 添加全局变量:

RelativeLayout ratafeia;

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);之前:

ratafeia = (RelativeLayout) findViewById(R.id.ratafeia);

mDrawerLayout.isDrawerOpen(mDrawerList);替换为mDrawerLayout.isDrawerOpen(ratafeia);

mDrawerLayout.closeDrawer(mDrawerList)
with mDrawerLayout.closeDrawer(ratafeia);

底线,用relativeLayout替换每个mDrawerList!

我已经过测试,它正在这里工作!

答案 1 :(得分:1)

只是给你一个想法,尝试下面的逻辑(使用ListView的包装器),它应该工作

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">


<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

   <RelativeLayout 
   android:layout_gravity="start"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    >
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#f4f8f9" />

<ImageView 
// Load your image here
with align parent bottom
/>
</RelativeLayout>

答案 2 :(得分:-1)

查看我自己创建的布局,以实现类似的事情,here.

此外,在关闭抽屉调用抽屉时,关闭(Gravity.START),不要在此处传递listView对象,而是传递相对布局。