繁体   English   中英

如何将图像添加到“抽屉”视图的底部?

[英]how can I add an image to the bottom of a Drawer view?

因此,我希望能够将徽标图像与列表分开放在抽屉视图的底部。 我见过人们用其他列表视图做类似的事情,诸如此类,但是每当我尝试实现它时,我都会遇到很多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);
    }
}

在您的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);之前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!

我已经测试过,并且可以在这里工作!

只是给您一个想法,请尝试以下逻辑(对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>

此处查看我为实现类似效果而创建的布局

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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