理解 View.setTranslationY() [/X() ] [英] understanding View.setTranslationY() [ /X() ]

查看:28
本文介绍了理解 View.setTranslationY() [/X() ]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想了解该方法到底在做什么.

as the title says i want to understand what that method exactly is doing.

首先我已经仔细检查了android的坐标系是这样工作的:坐标系

firstable i have double checked that the coordinate system of android works like that: coordinate system

次要 - 请花一点时间检查我的 android studio 屏幕,以及方法快速文档.为什么我的视图值(点击后)是 106?安卓屏幕

secondary - please take a minute to check my android studio screen, and the method quick doc. why is that value of my view (after clicking it) is 106? android screen

推荐答案

坐标系正确.

getY() 将返回 View 顶部的值 + Y 翻译.因此,如果 getY() 返回 106 并且您将转换 y 设置为 10,则视图的顶部应该在 96.尝试调用 getTop() 并检查什么是那个值

getY() will return the value of the top of your View + the Y translation. so, if getY() is returning 106 and you set translation y to 10, the top of your view should be at 96. try calling also getTop() and check what is that value

平移是应用于View 位置的偏移量.如果布局将您的 View 放在 x;y 并且您调用 setTranslationY(10),您的 View 将出现在 x;y+10.这是一种控制布局后View定位

the translation is an offset that is applied to the position of the View. if the layout place your View at x;y and you call setTranslationY(10), your View will appear at x;y+10. it's a way to control the positioning of the View after the layout

额外提示,不要记录所有内容,而是使用调试器

bonus tip, instead of logging everything, use the debugger

如果你仍然对位置和翻译之间的差异有疑问,你可以试试这个,创建一个空的活动并设置这个布局

in case you still have doubts about the difference between the position and translation, you could try this, create an empty activity and set this layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lelloman.dummy.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asd asd"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="asd"/>

</RelativeLayout>

然后,您将看到 TextView 位于 Button 的正上方,因为这是 RelativeLayout 将定位 的方式给定这些布局参数的视图.现在,尝试调用

then, you will see that the TextView is right above the Button, because this is how the RelativeLayout will position the Views given these layout parameters. now, try to call

findViewById(R.id.button).setTranslationY(100);

您会注意到按钮将向下移动 100px,但 TextView 仍将位于旧位置,因为在布局之后应用了翻译.它是 View 的特定内容,在 View 在其父级中的定位没有考虑到

you will notice that the button will be moved down by 100px, but the TextView will still be at the old position, because the translation is applied after the layout. it is something specific to that View that is not taken into account for the positioning of the View within its parent

你也可以用

<Button
    android:translationY="100px"
    ...

这篇关于理解 View.setTranslationY() [/X() ]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆