setTranslation没有工作布局android

时间:2014-07-03 09:15:37

标签: android android-layout android-activity

这是setTranslation的代码。但交易不起作用。此代码调用了side活动。这有什么问题。相同的代码在活动中完美运行。

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;
        }
    });

1 个答案:

答案 0 :(得分:0)

将activity_home中的所有内容移动到FrameLayout中并翻译该布局中的内容。

假设您的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>