首先我们看下效果图
这是最基本的横向滑动菜单

有人会说应该不会适配,所以我加了AutoLayout做了适配,首先我们在Application的onCreate() 方法中对AutoLayout操作;
public class MainAppliction extends Application{
@Override
public void onCreate() {
super.onCreate();
//autoLayout初始化
AutoLayoutConifg.getInstance().useDeviceSize().init(this);
}
}
然后在AndroidManifest.xml中引用MainAppliction,并且设置
<!--outoLayout分辨率-->
android:name="design_width"
android:value="750"/>
android:name="design_height"
android:value="1334"/>
最后,我们在Activity里面操作了
horizontalBg.setAdapter(getNewBean(),newOnEditAttributeLstener() {
/**
*
*@return0、不设置背景、1设置背景颜色、2、设置背景图片
*/
@Override
public int getViewType() {
return2;
}
@Override
public int getVewHeight() {
return 80;//设置菜单栏的高度
}
@Override
public boolean isAdjustModel() {
return true;//是否自适应
}
@Override
public int getSelectBgRes() {
return R.drawable.shape_bg_red;//设置选中item的背景
}
@Override
public int getNotSelectBgRes() {
return 0;//设置未选中item的背景
}
@Override
public int getSelectTextColor() {
return R.color.colorWrite;//设置选中item 字体颜色
}
@Override
public int getNotSelectTextColor() {
return R.color.textMain;//设置未选中item 字体颜色
}
@Override
public boolean isShowLine() {
return false;//隐藏下划线
}
});
另外附上所有item样式方法,这些属性应该够基本的Menu菜单使用
public interface OnEditAttribute{
/**
* 是否自适应
*@return
*/
boolean isAdjustModel();
/*** ======设置View属性 ********/
/**
*获得View的属性类型
*@return
*/
int getViewType();
/**
* 改变view高度
*@return
*/
int getVewHeight();
/**
* 改变view高度
*@return
*/
int getVewWidth();
/**
* 获取选中的背景颜色
*@return
*/
int getSelectBgColor();
/**
* 获取未选中的背景颜色
*@return
*/
int getNotSelectBgColor();
/**
* 获取选中的背景颜色
*@return
*/
int getSelectBgRes();
/**
* 获取未选中的背景颜色
*@return
*/
int getNotSelectBgRes();
/*** ======设置Item属性 ********/
/**
*获取选中的字体颜色
*@return
*/
int getSelectTextColor();
/**
*获取未选中的字体颜色
*@return
*/
int getNotSelectTextColor();
/**
* 获取字体大小
*@return
*/
int getTextSize();
/**
* 获取Item高度
*@return
*/
int getItemHeight();
/*** ======设置bottom属性 ********/
/**
*获得底部下划线的属性类型
*@return
*/
int getBottomViewType();
/**
* 是否显示下划线
*@return
*/
boolean isShowLine();
/**
* 改变bottom下划线高度
*@return
*/
int getBottomHeight();
/**
* 改变bottom宽度
*@return
*/
int getBottomWidth();
/**
* 设置底部下划线背景颜色
*@return
*/
int getBottomBackgoundColor();
/**
* 设置底部图片
*@return
*/
int getBottomDrawable();
}
也许有人会说UI给了奇葩的menu菜单怎么办,不要慌,这里还可以自定义Item布局

代码也是非常简单的,红色小框框的是你自己写的Item布局,黑色小框框是你的Item数据;红色大框框是你的点击事件

除此之外,你还可以写横向滚动器

网友评论