android.widget.TextView.setTextAlignment(): java.lang.NoSuchMethodError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20103079/
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-08-12 23:10:09  来源:igfitidea点击:

android.widget.TextView.setTextAlignment(): java.lang.NoSuchMethodError

javaandroidtextviewadtandroid-xml

提问by Paulo Roberto Rosa

Situation:

情况:

I have a TextViewthat have the property

我有一个TextView有财产的

android:textAlignment="center"

I am generating another TextViewdinamically, based on my TextViewfrom XML Layout, using a clone, cloning all the basic properties to work the way above.

我正在生成另一个TextView动态,基于我TextView的 XML 布局,使用克隆,克隆所有基本属性以按上述方式工作。

Problem:

问题:

To do this i need to use this method:

为此,我需要使用此方法:

this.myTextView.setMyTextViewProperty(MyTextView.getMyTextViewProperty());

for example:

例如:

this.MyTextView.setText(MyTextView.getText());

Note that this.MyTextViewis a local variable and MyTextViewis a private var declared on the top of the file, under class name.

请注意,这this.MyTextView是一个局部变量,并且MyTextView是在文件顶部、类名下声明的私有变量。

I do this on all the properties of the TextViewbut when i hit the following line of code from the TextAlignmentproperty...:

我对所有属性执行此操作,TextView但是当我从TextAlignment属性中点击以下代码行时...:

this.myTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

I tried to set it to a Custom Aligment instead of getting from my XML TextViewIt gives me an error:

我试图将它设置为自定义对齐而不是从我的 XML 获取TextView它给了我一个错误:

11-20 15:27:04.460: E/AndroidRuntime(9185): FATAL EXCEPTION: main
11-20 15:27:04.460: E/AndroidRuntime(9185): java.lang.NoSuchMethodError: android.widget.TextView.setTextAlignment

But i see on Ctrl+ Spacethat the method exists, so i cant understand what is happening.

但是我在Ctrl+ 上Space看到该方法存在,所以我无法理解发生了什么。

A second try was, to set my TextViewproperty to the property that comes from my TextView:

第二次尝试是,将我的TextView属性设置为来自我的属性TextView

this.myTextView.setTextAlignment(MyTextView.getTextAlignment());

With no success, too.

也没有成功。

Obs: i do not want a Android XML Layout solution, i want a solution on code, because i generate the TextViewdinamically on the Activity

Obs:我不想要一个 Android XML 布局解决方案,我想要一个关于代码的解决方案,因为我TextViewActivity

I'm using API Level 15

我正在使用 API 级别 15

Any help?

有什么帮助吗?

采纳答案by developeralways

The setTextAlignment method was added in API level 17. Maybe your compiler in the IDE is above 17 and the device/emulator which you are testing is less than that. Here the link to setTextAlignment.

setTextAlignment 方法是在 API 级别 17 中添加的。可能您的 IDE 中的编译器是 17 级以上,而您正在测试的设备/模拟器低于该值。这里是setTextAlignment链接

Added from the comments:

从评论中补充:

For API level 15 and below, you want setGravity, per this question.

对于 API 级别 15 及以下,您需要setGravity,根据这个问题