1、改变层次
首先要明白,android的层次由摆放view的先后顺序决定,也就是addView中的index,0表示最下面,越大越上面,不会超过容器的包含的View个数,(因为是数组下标)。
1、所以需要第一种方法,是最原始的方法。将一个view remove掉然后再加入进来,因为越后面加入的view,在越下面。
如下代码展示了如何交换两个ImageView的层次。
int screen_w = getResources().getDisplayMetrics().widthPixels; int screen_h = getResources().getDisplayMetrics().heightPixels; mContent.removeAllViews(); RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mExample1.getLayoutParams(); RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) mExample2.getLayoutParams(); mExample1 = new ImageView(MainActivity.this); mExample2 = new ImageView(MainActivity.this); mExample1.setImageResource(R.drawable.example_h); mExample2.setImageResource(R.drawable.example_v); layout(layoutParams1, layoutParams2); if (mExample2OnTop) { layoutParams1.width = screen_w / 4; layoutParams1.height = screen_h / 4; layoutParams2.width = screen_w; layoutParams2.height = screen_h; mExample1.setLayoutParams(layoutParams1); mExample2.setLayoutParams(layoutParams2); mContent.addView(mExample2); mContent.addView(mExample1); mExample2OnTop = false; } else { layoutParams1.width = screen_w; layoutParams1.height = screen_h; layoutParams2.width = screen_w / 4; layoutParams2.height = screen_h / 4; mExample1.setLayoutParams(layoutParams1); mExample2.setLayoutParams(layoutParams2); mContent.addView(mExample1); mContent.addView(mExample2); mExample2OnTop = true; } mExample2.setOnClickListener(ImageSwitchListener); mExample1.setOnClickListener(ImageSwitchListener);
int screen_w = getResources().getDisplayMetrics().widthPixels; int screen_h = getResources().getDisplayMetrics().heightPixels; RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mExample1.getLayoutParams(); RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) mExample2.getLayoutParams(); layout(layoutParams1, layoutParams2); mContent.requestLayout(); if (mExample2OnTop) { layoutParams1.width = screen_w / 4; layoutParams1.height = screen_h / 4; layoutParams2.width = screen_w; layoutParams2.height = screen_h; mExample1.setLayoutParams(layoutParams1); mExample2.setLayoutParams(layoutParams2); mExample1.bringToFront(); mExample2OnTop = false; } else { layoutParams1.width = screen_w; layoutParams1.height = screen_h; layoutParams2.width = screen_w / 4; layoutParams2.height = screen_h / 4; mExample1.setLayoutParams(layoutParams1); mExample2.setLayoutParams(layoutParams2); mExample2.bringToFront(); mExample2OnTop = true; }
private void moveToBack(View currentView) { ViewGroup viewGroup = ((ViewGroup) currentView.getParent()); int index = viewGroup.indexOfChild(currentView); for(int i = 0; i<index; i++) { viewGroup.bringChildToFront(viewGroup.getChildAt(i)); } }
if (mExample2OnTop) { // mExample1.bringToFront(); mExample1.setZ(100.f); mExample2.setZ(0.f); mExample2OnTop = false; } else { // mExample2.bringToFront(); mExample1.setZ(0.f); mExample2.setZ(100.f); mExample2OnTop = true; }这个接口比较新。作用是体现出来了。但是副作用还不知道。
不过需要注意的是,需要注意到父容器是什么类型的,FrameLayout,LinearLayout,RelativeLayout等,因为不同类型的LayoutParams是不同的
</pre><pre class="java" name="code"> private void layout(RelativeLayout.LayoutParams layoutParams1, RelativeLayout.LayoutParams layoutParams2) { layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); }
下面是如何修改View大小的代码
RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mExample1.getLayoutParams(); RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) mExample2.getLayoutParams(); layoutParams1.width = screen_w / 4; layoutParams1.height = screen_h / 4; layoutParams2.width = screen_w; layoutParams2.height = screen_h; mExample1.setLayoutParams(layoutParams1); mExample2.setLayoutParams(layoutParams2);
mExample1 = new Button(this); mExample2 = new Button(this); RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(screen_w, screen_h); RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(screen_w / 4, screen_h / 4); layout(layoutParams1, layoutParams2); mExample1.setLayoutParams(layoutParams1); mExample2.setLayoutParams(layoutParams2); mContent.addView(mExample1); mContent.addView(mExample2);