我自定义了一个ViewGroup.通过addView进行添加子View却发现,居然在不同的系统版本下显示不一样呀。
android4.4的view出现在上层;
android6.6的view出现在底层
按理说:addView()的循序不会改变呀。怎么搞得
能不能将代码给贴出来,看看
public class ScrollMenuView extends ViewGroup {
private final String TAG = "ScrollMenuView";
private Context mContext;
private final ArrayList<View> mMenusView = new ArrayList<>();
private MenuItemClickListener mMenuItemClickListener;
private View mBodyView;
private boolean target = false;
public ScrollMenuView(Context context) {
this(context, null, 0);
}
public ScrollMenuView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
for (int i = childCount-1; i >= 0; i--) {
measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int width = getMeasuredWidth();
int height = getMeasuredHeight();
if (getChildCount() >= 2) {
int size = mMenusView.size();
for (int i = 0; i < size; i++) {
View targetView = mMenusView.get(i);
View rightView = null;
if (i != 0) {
rightView = mMenusView.get(i - 1);
}
int r = width;
if (rightView != null) {
r = rightView.getLeft();
}
targetView.layout(r - targetView.getMeasuredWidth(), 0,
r, height);
System.out.printf("\n\ti=%s:t=%s,l=%s,r=%s,b=%s\n",
i,
targetView.getTop(),
targetView.getLeft(),
targetView.getRight(),
targetView.getBottom()
);
}
if (mBodyView != null) {
mBodyView.layout(0, 0, width, height);
}
}
}
public void addMenuView(final View itemView) {
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mMenuItemClickListener != null) {
mMenuItemClickListener.onClick(itemView, mMenusView.size());
}
}
});
ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
if (layoutParams == null) {
layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
}
itemView.setLayoutParams(layoutParams);
mMenusView.add(itemView);
addView(itemView);
}
public void addMenuView(int drawableId, String text) {
final Button menuBtn = new Button(mContext);
menuBtn.setBackgroundResource(R.drawable.btn_bg);
Drawable drawable = mContext.getResources().getDrawable(drawableId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
menuBtn.setCompoundDrawables(null, drawable, null, null);
int pd = DensityUtils.dp2px(mContext, 20);
menuBtn.setPadding(0, pd, 0, 0);
menuBtn.setCompoundDrawablePadding(2);
menuBtn.setText(text);
menuBtn.setTextSize(12);
menuBtn.setGravity(Gravity.CENTER);
menuBtn.setTextColor(mContext.getResources().getColor(R.color.cornell_red2));
LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
menuBtn.setLayoutParams(params);
menuBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mMenuItemClickListener != null) {
mMenuItemClickListener.onClick(menuBtn, mMenusView.size());
}
}
});
addView(menuBtn);
mMenusView.add(menuBtn);
}
public void setMenuBody(View body) {
this.mBodyView = body;
LayoutParams layoutParams = body.getLayoutParams();
if (layoutParams == null) {
layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
}
this.mBodyView.setLayoutParams(layoutParams);
addView(mBodyView);
}
public void setMenuItemClickListener(MenuItemClickListener menuItemClickListener) {
mMenuItemClickListener = menuItemClickListener;
}
interface MenuItemClickListener {
void onClick(View view, int index);
}
}
使用
mScrollMenuView = (ScrollMenuView) findViewById(R.id.scroll_menu);
mScrollMenuView.addMenuView(R.mipmap.menu_delete,
"delete1");
mScrollMenuView.addMenuView(R.mipmap.menu_delete,
"delete2");
View view = LayoutInflater.from(this).inflate(R.layout.item_current_file, null);
// view.setBackgroundColor(getResources().getColor(R.color.charade));
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);
mScrollMenuView.setMenuBody(view);
关键是在不同的android系统出现了不一样的显示。