ImageView不在下面列表视图中导航抽屉

问题描述:

我有一个400 x 206图像,我试图在我的导航抽屉中的菜单下方显示,但它不显示当我打开导航抽屉时。在AS设计预览中奇怪的是,它看起来像图像正在转换成一条线。我现在也发现我的敞开式抽屉在最右边大约半英寸宽的地方有一个额外的透明空间条。我在想这两者是相关的,但我目前的首要任务是弄清楚如何让图像显示在菜单项下方。ImageView不在下面列表视图中导航抽屉

activity_main.xml的Android Studio设计预览。顶部蓝色的轮廓是去一直到右边是left_drawer,对象是在它无法很好地去,因为右边是left_drawer_listview,和线下是drawer_buttom_image:

enter image description here

而且对于我看过的Background image and image view in Navigation Drawer的记录要复杂得多,所以我可能在为我的需求简化解决方案时犯了一个错误。

activity_main.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"> 

    <!-- As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- android:layout_gravity="start" tells DrawerLayout to treat 
     this as a sliding drawer on the left side for left-to-right 
     languages and on the right side for right-to-left languages. 
     The drawer is given a fixed width in dp and extends the full height of 
     the container. --> 
    <LinearLayout 
     android:id="@+id/left_drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:orientation="vertical"> 

     <ListView 
      android:id="@+id/left_drawer_listview" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:background="@color/secondary_blue_transparent" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" /> 


     <ImageView 
      android:id="@+id/drawer_bottom_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/testimage" 
      android:contentDescription="@string/testdescr"></ImageView> 

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

drawer_list_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="60dp"> 

    <ImageView 
     android:id="@+id/drawer_icon" 
     android:layout_width="40dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/selector" 
     android:contentDescription="@string/app_name" 
     android:paddingLeft="10dp" 
     android:paddingRight="5dp" /> 

    <TextView 
     android:id="@+id/drawer_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/drawer_icon" 
     android:background="@drawable/selector" 
     android:gravity="center_vertical" 
     android:minHeight="?android:attr/listPreferredItemHeightSmall" 
     android:paddingLeft="10dp" 
     android:paddingRight="5dp" 
     android:textAppearance="?android:attr/textAppearanceListItemSmall" 
     android:textColor="#fff" /> 

</RelativeLayout> 

MainActivity代码:

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

     dataList = new ArrayList<DrawerItem>(); 
     mTitle = mDrawerTitle = getTitle(); 
     mDrawerTitles = getResources().getStringArray(R.array.drawer_array); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer); 
     mDrawerListView = (ListView) findViewById(R.id.left_drawer_listview); 

     // set a custom shadow that overlays the main content when the drawer opens 
     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 

     for (int i = 0; i < mDrawerTitles.length; ++i) { 
      dataList.add(new DrawerItem(mDrawerTitles[i], mDrawerIcons[i])); 
     } 

     // set up the drawer's list view with items and click listener 
     drawerAdapter = new CustomDrawerAdapter(this, R.layout.drawer_list_item, 
       dataList); 
     mDrawerListView.setAdapter(drawerAdapter); 
/*  mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mDrawerTitles));*/ 
     mDrawerListView.setOnItemClickListener(new DrawerItemClickListener()); 

     // enable ActionBar app icon to behave as action to toggle nav drawer 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     // ActionBarDrawerToggle ties together the the proper interactions 
     // 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().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      selectItem(1); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    /* Called whenever we call invalidateOptionsMenu() */ 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // If the nav drawer is open, do x 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear); 

     return super.onPrepareOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // The action bar home/up action should open or close the drawer. 
     // ActionBarDrawerToggle will take care of this. 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     // Handle action buttons 
     switch (item.getItemId()) { 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    private void selectItem(int position) { 
     // update the main content by replacing fragments 
     //Fragment fragment = new PlanetFragment(); 
     Fragment fragment = new EventFragment(); 
     Bundle args = new Bundle(); 

/*  // Pass in variables 
     args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); 
     fragment.setArguments(args);*/ 

     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

     // update selected item and title, then close the drawer 
     mDrawerListView.setItemChecked(position, true); 
     //setTitle(mDrawerTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerLinear); 
    } 

这是因为ListView已经使用所有可用的高度。将ListView高度更改为0dp并添加1的权重。这导致它使用所有可用的高度,除了已经使用的高度。

<ListView 
     android:id="@+id/left_drawer_listview" 
     android:layout_width="240dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/secondary_blue_transparent" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 
+0

谢谢!你有没有关于如何修改ImageView以获取最右边抽屉的额外透明纸条的建议? – 2014-09-05 20:27:59