0

我試圖想出如何通過https://github.com/mikepenz/MaterialDrawerMaterialDrawer如何自定義小抽屜

定製MaterialDrawer在時刻寬度太大。 我想用更大的圖標來縮小寬度以覆蓋空格並更改背景顏色。

此應用程序將僅適用於平板電腦。 謝謝。

這裏是我的抽屜:

<LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/toolbar"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <!-- Place your content here --> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" 
       android:id="@+id/button" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentStart="true" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" 
       android:id="@+id/button2" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentEnd="true" 
       android:layout_marginBottom="215dp" /> 
     </RelativeLayout> 
    </LinearLayout> 


result = new DrawerBuilder() 
       .withActivity(this) 
       .addDrawerItems(
         new PrimaryDrawerItem().withName("1").withIcon(FontAwesome.Icon.faw_home).withIdentifier(1), 
         new PrimaryDrawerItem().withName("2").withIcon(FontAwesome.Icon.faw_home).withBadge("22").withBadgeStyle(new BadgeStyle(Color.RED, Color.RED)).withIdentifier(2), 
         new PrimaryDrawerItem().withName("3").withIcon(FontAwesome.Icon.faw_home).withIdentifier(3) 
       ) // add the items we want to use with our Drawer 
       .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { 
        @Override 
        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { 
         if (drawerItem instanceof Nameable) { 
          Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show(); 
         } 
         return false; 
        } 
       }) 
       .withGenerateMiniDrawer(true) 
       .withSavedInstance(savedInstanceState) 
       // build only the view of the Drawer (don't inflate it automatically in our layout which is done with .build()) 
       .buildView(); 


     miniResult = result.getMiniDrawer(); 
     View view = miniResult.build(this); 

     LinearLayout container = (LinearLayout) findViewById(R.id.container); 
     container.addView(view, 0); //view is the view of your MiniDrawer 

圖片: enter image description here

我想建立這樣的事情: enter image description here

感謝

本來我問了一個問題如何在這裏啓用固定的迷你抽屜 Android build mini navigation drawer with Icons

+0

所以你不僅要使它更小,還要比原來的72dp更大? – mikepenz

+0

是的,我希望能夠手動更改迷你抽屜的寬度。然後再將圖標設置爲數學抽屜寬度。 – MrG

+0

你需要額外的信息嗎? – mikepenz

回答

1

這個答案是基於版本> = v5.3.5

首先,我們必須定義爲MiniDrawer本身的寬度。在下面顯示的代碼示例中,我們將配置MiniDrawer及其項目採取144dp。我建議在將View添加到您的佈局之前執行此操作,如此示例中所示。

//get the MiniDrawer 
MiniDrawer miniDrawer = result.getMiniDrawer(); 
//build it and get the view 
View miniDrawerView = miniDrawer.build(this); 
//define the width (You could also do it via the LayoutParams 
miniDrawerView.setMinimumWidth((int) UIUtils.convertDpToPixel(144, this)); 
//add the MiniDrawer to your view hirachy 
findViewById(R.id.frame_container)).addView(miniDrawerView, 0); 

後已添加的MiniDrawer視圖本身佈局你想通過你的dimens.xml文件通過定義來定義MiniDrawerItems你可以做到這一點的改變尺寸如下:

<!-- required for a changed size --> 
<!-- 144dp = the full width of the MiniDrawer --> 
<dimen name="material_mini_drawer_item">144dp</dimen> 
<!-- 144dp - 8dp padding around = 128dp --> 
<dimen name="material_mini_drawer_item_icon">128dp</dimen> 
<!-- 144dp - 16dp padding around = 112dp --> 
<dimen name="material_mini_drawer_item_profile_icon">112dp</dimen> 

<!-- optional configurable dimensions --> 
<dimen name="material_mini_drawer_item_padding">4dp</dimen> 
<dimen name="material_mini_drawer_item_padding_sides">8dp</dimen> 
<dimen name="material_mini_drawer_item_icon_padding">16dp</dimen> 
<dimen name="material_mini_drawer_item_badge_text">12sp</dimen> 
<dimen name="material_mini_drawer_item_profile_icon_padding">16dp</dimen> 

在此之後您的MiniDrawer和項目將以正確的大小顯示。