java 如何使用 Android setTranslation ?(或另一种移动文本视图的方法?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14158413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:17:21  来源:igfitidea点击:

How to use Android setTranslation ? (or another way to move a textview ?)

javaandroidtextview

提问by user1948708

I want to start to make a "spirit level" app (not sure of this word, the tool to make sure the surface you put it on is even(straight)) for school. For now I just want to move textviews then I'll had the sensor listener to do it automatically then i'll turn them to images, etc..

我想开始为学校制作一个“精神层面”的应用程序(不确定这个词,确保你把它放在表面上的工具是均匀的(直的))。现在我只想移动文本视图,然后我会让传感器监听器自动执行它,然后我会将它们转换为图像等。

Anyway, I can't move the textView in the screen!

无论如何,我无法在屏幕中移动textView!

I found a method in view, called setTranslationX and setTranslationY who seem to do exactly what I want, so I tried to use them this way:

我在视图中找到了一个名为 setTranslationX 和 setTranslationY 的方法,它们似乎完全符合我的要求,所以我尝试以这种方式使用它们:

super.onCreate(savedInstanceState);

float nombre=100;
setContentView(R.layout.activity_main);

TextView H = (TextView) findViewById(R.id.Champ_H);

H.setTranslationX(nombre);

To just move a textView to the right.

只需将 textView 向右移动。

But I've got this message for the last line:

但我在最后一行收到了这条消息:

The method setTranslationX(float) is undefined for the type TextView

未定义 TextView 类型的 setTranslationX(float) 方法

The problem is, yes, it is, I looked the documentation, this method exists and takes a float as a parameter. I've of course imported android.view.View which is supposed to contain it, but that won't work. I've also tried to use setLeft which does a similar thing but the same problem occurs. It seems that every method what would allow me to move a textView, eclipse won't let me use them.

问题是,是的,是的,我查看了文档,此方法存在并采用浮点数作为参数。我当然已经导入了应该包含它的 android.view.View ,但这不起作用。我也尝试使用 setLeft 做类似的事情,但发生了同样的问题。似乎每种允许我移动 textView 的方法,eclipse 都不会让我使用它们。

Does anybody know why? Or know an other way to move a TextView?

有人知道为什么吗?或者知道另一种移动 TextView 的方法?

回答by Dipendra

Use nineOldAndroid.jarfile in your project And use this way

nineOldAndroid.jar在你的项目中使用文件并使用这种方式

import com.nineoldandroids.view.ViewHelper;


ViewHelper.setTranslationX(myView, translation);

回答by Maury

Try this:

试试这个:

h.setTranslationX(100f);

in my eclipse is work fine:)

在我的日食中工作正常:)

回答by xaviersjs

You may need to specify the unit to translate. try:

您可能需要指定要翻译的单位。尝试:

H.setTranslationX(100px);

see http://developer.android.com/reference/android/view/View.html#attr_android:translationX

http://developer.android.com/reference/android/view/View.html#attr_android:translationX