在自定义视图中设置文本

发布于 2024-10-22 03:06:18 字数 756 浏览 6 评论 0原文

我根据在线教程创建了一个自定义课程。它工作得很好,但我想做的是让我可以设置视图的文本,就像我设置任何其他视图一样。这是如何实现的。这是自定义视图的 xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/file_image_holder"
android:layout_width="110dip"
android:layout_height="110dip"
android:src="@drawable/pdf_icon" />
<TextView
android:id="@+id/file_name_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Name Here" />
</LinearLayout>

我想要实现的是这个

FileView fileView = new FileView(this)
fileView.setText("text")

I have created a custom class from a tutorial online. It works fine, but what I want to do is make it so that I can set the text of the view, just like I would any other view. How is that accomplished. Here is the xml for the custom view

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/file_image_holder"
android:layout_width="110dip"
android:layout_height="110dip"
android:src="@drawable/pdf_icon" />
<TextView
android:id="@+id/file_name_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Name Here" />
</LinearLayout>

What I am trying to achieve is this

FileView fileView = new FileView(this)
fileView.setText("text")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美不过初阳 2024-10-29 03:06:18
TextView filename = (TextView)findViewById(R.id.file_name_holder);
filename.setText("someFile.exe");

另一方面,您可以使用提示为用户提供灰色文本提示,他们在输入文件名时不必删除该提示。在 XML 中,将 TextView 的 android:text 替换为 android:hint

TextView filename = (TextView)findViewById(R.id.file_name_holder);
filename.setText("someFile.exe");

On another note ... you can use hint to give your users a greyed out text prompt that they won't have to erase when entering a filename. In your XML replace android:text with android:hint for your TextView.

十年九夏 2024-10-29 03:06:18

将“File Name Here”替换为“TEXT YOU WANT”或@string/name_of_string_in_string.xml

或java方式,这是其他人回答的

replace "File Name Here" with "TEXT YOU WANT" or @string/name_of_string_in_string.xml

or the java way, which someone else answered

神仙妹妹 2024-10-29 03:06:18

这里 no_internet 是包含 xml 视图。在包含视图中包含文本视图。您可以在包含自定义文本视图中设置文本,例如...

 no_internet     = view.findViewById(R.id.no_internet);
  ((TextView) no_internet).setText(R.string.no_internet_connection);

here no_internet is include xml view . in the include view contain text view. you can set text in include custom text view like ....

 no_internet     = view.findViewById(R.id.no_internet);
  ((TextView) no_internet).setText(R.string.no_internet_connection);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文