自定义布局
package com.example.aiqiyi.view_ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
//HorizontalScrollView就是水平划动
public class Sidngmenu extends HorizontalScrollView {
private LinearLayout mWapper;//固顶的HorizontalScrollView水平滚动条
private ViewGroup mMenu;//菜单蓝的子类
private ViewGroup Content;
private int mScreenWidth;//屏幕宽度
private int mMenuMidth;//设置menu的宽
//dp
private int mMenuRightpadding=50;//右侧边距
private boolean once;//为了只设置一次宽和高
//使用自定义属性时,调用
public Sidngmenu(Context context, AttributeSet attrs) {//attrs布局里面的内容
super(context, attrs);
WindowManager systemService = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
DisplayMetrics outMetrics=new DisplayMetrics();
systemService.getDefaultDisplay().getMetrics(outMetrics);//获得了屏幕的尺寸
mScreenWidth=outMetrics.widthPixels;//宽度的一个像素值
// dp转换px(像素)
// -COMPLEX_UNIT_DIP意思是dp--COMPLEX_UNIT_SP也算是sp
// 一般这个封装成方法用来转换像素,第二个参数是转换多大的值
// 把50(第二个参数)dp(第一个参数)转换成了50px
mMenuRightpadding=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
context.getResources().getDisplayMetrics());
// TypedValue通过applyDimension50转换成了像素值
}
//自定义ViewGroup
//设置子view宽和高也就设置自己的宽和高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
if(!once){
//获得(com.example.aiqiyi.view_ui.Sidngmenu)里面的布局
mWapper=(LinearLayout) getChildAt(0);//里面布局第一个
// mMenu第一个元素是旁边的视图
mMenu=(ViewGroup) mWapper.getChildAt(0);//mWapper布局里面第一个元素
// Content第二个元素是主的视图
Content=(ViewGroup) mWapper.getChildAt(1);//mWapper布局里面第二个元素
// 设置宽度为屏幕的宽度(mScreenWidth宽度的像素值-mMenuRightpadding(50)宽度的像素值)
mMenuMidth=mMenu.getLayoutParams().width=mScreenWidth-mMenuRightpadding;
Content.getLayoutParams().width=mScreenWidth;
once=true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
//决定子View的摆放位置
//通过设置偏移量,将menu隐藏
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
if(changed){//当前不发生任何变化就不叼用
//快速隐藏(x为正值滚动条会向右移动,内容向左移动(是横轴)y反之(是纵轴))
this.scrollTo(mMenuMidth, 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
int action=ev.getAction();
switch(action){//判断用户的操作
case MotionEvent.ACTION_UP://滑动
//隐藏在左边的宽度scrollx隐藏
int scrollx=getScrollX();
if(scrollx>=mMenuMidth/2){//scrollx大于mMenuMidth的一般
//smoothScrollTo(x, y);慢慢隐藏
this.smoothScrollTo(mMenuMidth, 0);
}else{
this.smoothScrollTo(0, 0);//-1?
}
return true;
}
return super.onTouchEvent(ev);
}
}
使用自定义的布局.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.aiqiyi.view_ui.Sidngmenu
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content" //内容宽度(要加载"@layout/leimen!不能用填充父类的宽度"(包含了两个布局))//
android:layout_height="match_parent"
android:orientation="horizontal"
>
<include layout="@layout/leimen"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/asdfasdf"//主页面的背景
></LinearLayout>
</LinearLayout>
</com.example.aiqiyi.view_ui.Sidngmenu>
</RelativeLayout>
布局效果图
image.png
侧滑旁边按钮布局.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/llllll">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/rank_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="41dp"
android:text="@string/enter"
android:textColor="#000"
android:onClick="rank_1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/rank_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_btn_up"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/rank_3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_btn_up"
/>
</RelativeLayout>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/set"
android:onClick="button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:textSize="15sp"
android:text="@string/set"
android:onClick="button1"
/>
</RelativeLayout>
效果图
image.png
最终结果
image.png