【问题标题】:setTranslation not working layout androidsetTranslation 不工作布局android
【发布时间】:2014-07-03 09:15:37
【问题描述】:

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

【问题讨论】:

  • 你怎么称呼它?你在哪里称呼它?当你调用它时会发生什么?你有什么错误吗?也许是崩溃?你检查过 Logcat 吗?
  • @WarrenFaith 我从服务中调用它。翻译值正确,但没有发生翻译。
  • 对此有任何帮助/

标签: android android-layout android-activity


【解决方案1】:

将 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2018-03-06
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多