2017-07-30 14 views
0

NavigationView私はカスタムリストディバイダを設定しようとしています。NavigationViewのリストディバイダの高さを設定する

私は、ファイル "drawer_list_divider.xml" 作った:android:theme="@style/NavigationViewTheme"

スタイル:

<style name="NavigationViewTheme" > <item name="android:listDivider">@drawable/drawer_list_divider</item> </style>

周器が得る私はこのようにそれを設定し

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
android:thickness="25dp"> 
<solid android:color="@color/secondaryBackground"/> 

を私が欲しい色ですが、影響を受けていない厚さではありません。仕切りを別の高さの矩形にしたいと思っています..どのように高さ/厚さを設定しますか?

+0

私はちょうどrecyclerviewを使用し、それに装飾を追加しました。 –

+0

@ManojFrekzzさんがコード例を使って答えを書くことができますか? –

答えて

0

ナビゲーションドロワーレイアウト

<android.support.v4.widget.DrawerLayout 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_below="@+id/appBar"> 
<!--Content--> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
    android:id="@+id/content_textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/app_name" /> 
</LinearLayout> 
<!-- Navigation Drawer--> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/drawerRecyclerView" 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="left" 
    android:background="#ffffff"> 
</android.support.v7.widget.RecyclerView> 

その後recyclerviewアダプタは、アダプタを設定すると

public class DrawerAdapter extends 
    RecyclerView.Adapter<DrawerAdapter.DrawerViewHolder> { 
private ArrayList<DrawerItem> drawerMenuList; 
    public DrawerAdapter(ArrayList<DrawerItem> drawerMenuList) { 
this.drawerMenuList = drawerMenuList; 
} 
@Override 
public DrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
View view; 
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false); 
return new DrawerViewHolder(view); 
} 
@Override 
public void onBindViewHolder(DrawerViewHolder holder, int position) { 
holder.title.setText(drawerMenuList.get(position).getTitle()); 
holder.icon.setImageResource(drawerMenuList.get(position).getIcon()); 
} 
@Override 
public int getItemCount() { 
return drawerMenuList.size(); 
} 
class DrawerViewHolder extends RecyclerView.ViewHolder { 
TextView title; 
ImageView icon; 
public DrawerViewHolder(View itemView) { 
    super(itemView); 
    title = (TextView) itemView.findViewById(R.id.title); 
    icon = (ImageView) itemView.findViewById(R.id.icon); 
}  }  } 

を設定しますが、ナビゲーション引き出しを表示したいました。 を設定中recyclerviewが仕切りを設定ここで

DrawerAdapter adapter = new DrawerAdapter(mDrawerItemList); 
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity())); 
mRecyclerView.setAdapter(adapter); 

がitemDecoratorクラスは

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration { 
private Drawable mDivider; 

public SimpleDividerItemDecoration(Context context) { 
    mDivider = context.getResources().getDrawable(R.drawable.recycler_horizontal_divider); 
} 

@Override 
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
    int left = parent.getPaddingLeft(); 
    int right = parent.getWidth() - parent.getPaddingRight(); 

    int childCount = parent.getChildCount(); 
    for (int i = 0; i < childCount; i++) { 
     View child = parent.getChildAt(i); 

     RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 

     int top = child.getBottom() + params.bottomMargin; 
     int bottom = top + mDivider.getIntrinsicHeight(); 

     mDivider.setBounds(left, top, right, bottom); 
     mDivider.draw(c); 
    } 
}} 

です。ここデバイダ描画可能です。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<size 
    android:width="1dp" 
    android:height="1dp" /> 

<solid android:color="#2EC590" /> 

</shape> 
+0

私はすぐにそれを試みますが、その前に、RecyclerViewではなくNavigationViewで同じ結果を達成する方法があると思いますか? –

+0

recyclerviewやその他のリストビューを使用しないと、分数幅を大きくすることはできません。 –

+0

OKこれはできますが、私はちょっとしたことがありません。ナビゲーションビューでできるような引き出しアイテムのグループを作成し、各アイテム間ではなく各グループ間にだけ仕切りを追加する方法はありますか? –

0

あなたがタグshapeにタグsizeではなくthicknessを使用する必要があります。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle" > 

    <size android:height="1dp" /> 

    <solid android:color="#10000000"/> 

</shape> 
+0

サイズは何にも影響しませんでした –

関連する問題