+-
![android – 理解View.setTranslationY()[/ X()] android – 理解View.setTranslationY()[/ X()]](/img/no_img.png)
正如标题所说,我想了解该方法到底在做什么.
我已经仔细检查了android的坐标系是这样的:
coordinate system
二级 – 请花一点时间检查我的android studio屏幕,方法快速doc.为什么我的观点(点击后)的价值是106?
android screen
最佳答案
坐标系是正确的.
getY()将返回View Y转换顶部的值.所以,如果getY()返回106并且您将平移y设置为10,则视图的顶部应为96.尝试调用getTop()并检查该值是什么
平移是应用于视图位置的偏移量.如果布局将View放在x; y并调用setTranslationY(10),则视图将显示在x; y 10.这是一种在布局后控制View的位置的方法
奖金提示,而不是记录所有内容,使用调试器
如果你仍然怀疑位置和翻译之间的区别,你可以尝试这个,创建一个空活动并设置这个布局
<?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在给定这些布局参数的情况下定位Views的方式.现在,试着打电话
findViewById(R.id.button).setTranslationY(100);
您会注意到该按钮将向下移动100px,但TextView仍将处于旧位置,因为在布局后应用了平移.它是特定于该视图的内容,不会将视图定位在其父视图中
你也可以在xml中设置翻译
<Button
android:translationY="100px"
...
点击查看更多相关文章
转载注明原文:android – 理解View.setTranslationY()[/ X()] - 乐贴网