使用bringChildToFront()将view显示在屏幕最前方

public abstract void bringChildToFront (View child)
把该视图置于其他所有子视图之上,如在FrameLayout中切换被叠放的视图。

该方法出自public interface ViewParent


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;

public class HelloWorld2 extends Activity {
/** Called when the activity is first created. */

AbsoluteLayout mLayoutGroup = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);

mLayoutGroup = new AbsoluteLayout(this);
AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams
(320, 480, 0, 0);

setContentView(mLayoutGroup, layoutParams);

Button button= new Button(this);
button.setText("testButton");
layoutParams = new AbsoluteLayout.LayoutParams(120, 60, 20, 20);
mLayoutGroup.addView(button, layoutParams);
button.setOnTouchListener(touchListener);

final Button btButton = new Button(this);
btButton.setText("测试按钮移动");
layoutParams = new AbsoluteLayout.LayoutParams(120, 60, 20, 160);
mLayoutGroup.addView(btButton, layoutParams);
btButton.setOnTouchListener(touchListener);
}

OnTouchListener touchListener = new OnTouchListener()
{
int temp[] = new int[]{0, 0};
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
int eventAction = arg1.getAction();
Log.e("testButtonMove", "OnTouchAction:"+eventAction);

int x = (int)arg1.getRawX();
int y = (int)arg1.getRawY();

switch (eventAction) {
case MotionEvent.ACTION_DOWN:

temp[0] = (int)arg1.getX();
temp[1] = (int)(y-arg0.getTop());

mLayoutGroup.bringChildToFront(arg0);
arg0.postInvalidate();

break;
case MotionEvent.ACTION_MOVE:

int left = x - temp[0];
int top = y - temp[1];
int right = left + arg0.getWidth();
int bottom = top + arg0.getHeight();

arg0.layout(left, top, right, bottom);
arg0.postInvalidate();

break;

default:
break;
}

return false;
}
};
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值