这是setTranslation的代码。但是转换不工作。这个代码在呼叫外部活动。有什么问题吗?同样的代码在一个活动中完美地工作。
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.activity_home, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(myView, params);
lockParent = myView.findViewById(R.id.lockParent);
lockParent.setOnTouchListener(new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
System.out.println(arg1.getAction());
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("down");
downX = arg1.getRawX();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("move");
deltaX = arg1.getRawX() - downX;
lockParent.setTranslationX(deltaX);
break;
default:
break;
}
return true;
}
});
将activity_home中的所有内容移动到framayout中,并翻译该布局中的内容。
假设你的activity_home布局是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lockParent>
...
<RelativeLayout>
修改activity_home布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lockParent>
...
<RelativeLayout>
<FrameLayout>