效果图
布局
主布局
<?xml version="1.0" encoding="utf-8"?>
<example.com.quarterhour.widget.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
tools:context="example.com.quarterhour.activity.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal">
<include layout="@layout/layout_menu" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/biaoti"
android:layout_width="match_parent"
android:layout_height="100px"
android:background="#03A9F4">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/user_t"
android:layout_width="80px"
android:layout_height="80px"
android:layout_centerVertical="true"
android:layout_marginLeft="45px"
fresco:placeholderImage="@mipmap/touxiang"
fresco:roundAsCircle="true"
fresco:roundedCornerRadius="90px" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="推荐"
android:textColor="#ffffff"
android:textSize="40px" />
<ImageView
android:id="@+id/edt_btn"
android:layout_width="50px"
android:layout_height="50px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="45px"
android:src="@mipmap/edt" />
</RelativeLayout>
<com.hjm.bottomtabbar.BottomTabBar
android:id="@+id/tabBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/biaoti"/>
</RelativeLayout>
</LinearLayout>
</example.com.quarterhour.widget.SlidingMenu>
菜单栏布局layout_menu.xml,看自己需求自己布局吧
上代码----->
public class SlidingMenu extends HorizontalScrollView {
/**
* 屏幕宽度
*/
private int mScreenWidth;
/**
* dp
*/
private int mMenuRightPadding = 40;
/**
* 菜单的宽度
*/
private int mMenuWidth;
private int mHalfMenuWidth;
private boolean once;
private boolean isOpen;
public SlidingMenu(Context context) {
this(context, null);
}
public SlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
mScreenWidth = DisplayMetricsUtil.getScreenWidth(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 显示的设置一个宽度
*/
if (!once) {
LinearLayout wrapper = (LinearLayout) getChildAt(0);
ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);
ViewGroup content = (ViewGroup) wrapper.getChildAt(1);
// dp to px
mMenuRightPadding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding, content
.getResources().getDisplayMetrics());
mMenuWidth = mScreenWidth - mMenuRightPadding;
mHalfMenuWidth = mMenuWidth / 2;
menu.getLayoutParams().width = mMenuWidth;
content.getLayoutParams().width = mScreenWidth;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
// 将菜单隐藏
this.scrollTo(mMenuWidth, 0);
once = true;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
// Up时,进行判断,如果显示区域大于菜单宽度一半则完全显示,否则隐藏
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();
if (scrollX > mHalfMenuWidth)
this.smoothScrollTo(mMenuWidth, 0);
else
this.smoothScrollTo(0, 0);
return true;
}
return super.onTouchEvent(ev);
}
/**
* 打开菜单
*/
public void openMenu() {
if (isOpen)
return;
this.smoothScrollTo(0, 0);
isOpen = true;
}
/**
* 关闭菜单
*/
public void closeMenu() {
if (isOpen) {
this.smoothScrollTo(mMenuWidth, 0);
isOpen = false;
}
}
/**
* 切换菜单状态
*/
public void toggle() {
if (isOpen) {
closeMenu();
} else {
openMenu();
}
}
}
如果想要点击控件实现侧滑菜单栏滑出效果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userT = (SimpleDraweeView) findViewById(R.id.user_t);
activityMain = (SlidingMenu) findViewById(R.id.activity_main);
userT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activityMain.toggle();
}
});
}