继承LinearLayout自定义左侧菜单

public class LeftMenuView extends LinearLayout {
LinkedHashMap<Integer,String> map=new LinkedHashMap<>();
public LeftMenuView(Context context) {
super(context);
addView(context);
}
public LeftMenuView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
addView(context);
}
public LeftMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
addView(context);
} private void addView(final Context mContext) {
map.put(R.drawable.ico_left_menu_cashier,"收银");
map.put(R.drawable.ico_left_menu_order,"订单");
map.put(R.drawable.ico_left_menu_after_sales,"售后");
map.put(R.drawable.ico_left_menu_goods,"商品");
map.put(R.drawable.ico_left_menu_setting,"设置");
for (final Map.Entry<Integer, String> entry : map.entrySet()) {
TextView tvName=new TextView(mContext);
View viewLine=new View(mContext);
addView(tvName);
addView(viewLine); LinearLayout.LayoutParams tvLayoutParams = (LayoutParams) tvName.getLayoutParams();
LinearLayout.LayoutParams viewLayoutParams = (LayoutParams) viewLine.getLayoutParams(); tvLayoutParams.width=LayoutParams.WRAP_CONTENT;
tvLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.left_menu_tv_heigth);
tvLayoutParams.gravity=Gravity.CENTER;
tvName.setText(entry.getValue());
tvName.setTag(entry.getKey());
tvName.setTextSize(getResources().getDimension(R.dimen.textsize_10));
tvName.setTextColor(ContextCompat.getColor(mContext,R.color.common_blue));
tvName.setGravity(Gravity.CENTER);
Drawable drawableLeft=ContextCompat.getDrawable(mContext,entry.getKey());
drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
tvName.setCompoundDrawables(drawableLeft,null,null,null);
tvName.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen._10dp)); viewLayoutParams.width= LayoutParams.MATCH_PARENT;
viewLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.common_margin_super_small);
viewLine.setBackgroundColor(ContextCompat.getColor(mContext,R.color.divider_line_grey_color));
tvName.setOnClickListener(new View.OnClickListener(){ @Override
public void onClick(View v) {
//listener.leftClick();
int drawableId=Integer.valueOf(v.getTag().toString());
if(drawableId==R.drawable.ico_left_menu_cashier){
mContext.startActivity(new Intent(mContext, MainActivity.class));
}else if(drawableId==R.drawable.ico_left_menu_order){ }else if(drawableId==R.drawable.ico_left_menu_after_sales){ }else if(drawableId==R.drawable.ico_left_menu_goods){
mContext.startActivity(new Intent(mContext, GoodsMessageActivity.class));
}else if(drawableId==R.drawable.ico_left_menu_setting){ }
}
});
tvName.setLayoutParams(tvLayoutParams);
viewLine.setLayoutParams(viewLayoutParams);
}
} //自定义的顶部ActionBar的点击监听;
private LeftMenuClickListener listener;
//点击事件的监听接口
public interface LeftMenuClickListener {
void leftClick();
void rightClick();
}
//提供activity调用的方法,类似于Button类的setOnClickListener(OnClickListener listener)
//传入具体实现方法
public void setOnTopbarClickListener(LeftMenuClickListener listener){
this.listener=listener;
} }

使用方法

    <com.freexiaoyu.app.widget.LeftMenuView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/linear_leftmenu"
android:layout_width="220.0dip"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/white"
android:orientation="vertical" />

  

上一篇:GNU/Linux下Freeplane的界面渲染问题


下一篇:(转)CSS颜色及标签超链接颜色改变