How to add divider to items in AndroidResideMenu?

元气小坏坏 提交于 2019-12-06 05:37:39

With ResideMenu, every item is an instance of ResideMenuItem which is basically a simple LinearLayout.

The solution I would use, is the following:

// create menu items;
itemHome     = new ResideMenuItem(this, R.drawable.icon_home,     "Home");
itemProfile  = new ResideMenuItem(this, R.drawable.icon_profile,  "Profile");
itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "Calendar");
itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "Settings");

itemHome.setOnClickListener(this);
itemProfile.setOnClickListener(this);
itemCalendar.setOnClickListener(this);
itemSettings.setOnClickListener(this);

resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);
resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);
resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);
resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);

// Force top margin for these elements
setMargin(itemHome);
setMargin(itemProfile);
setMargin(itemCalendar);
setMargin(itemSettings);

Use the following method to apply margins:

private void setMargin(ResideMenuItem item)
{
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) item.getLayoutParams();
    params.setMargins(0, dpToPx(15), 0, 0);
    item.setLayoutParams(params);
}

Don't forget to implement dpToPx to convert 15dp into the corresponding pixel size.


To add a divider line, you could use something like that:

private void addSeparatorAfter(ResideMenuItem menuItem)
{
    LinearLayout parent = (LinearLayout) ((ViewGroup) menuItem.getParent()).getParent();

    View separator = new View(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(2dp));
    params.setMargins(dpToPx(5), dpToPx(5), dpToPx(5), dpToPx(5));
    separator.setLayoutParams(params);
    separator.setBackgroundColor(0xFFCCCCCC);

    parent.addView(separator);
}

and call it right after each addMenuItem method.

For example, in your case, it would be:

resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);
addSeparatorAfter(itemHome);
resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);
addSeparatorAfter(itemProfile);
resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);
addSeparatorAfter(itemCalendar);
resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);

Try this

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/white"
        android:textSize="13sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_marginTop="4dp"
    android:background="#000000" >
</LinearLayout>

</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!