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