如何在Android应用程序中打印消息

时间:2015-12-10 20:03:45

标签: java android xml android-studio

您好我第一次开始学习如何编写Android应用程序,但我遇到了问题。

当我转到string.xml文件以输入要在屏幕上打印的消息时。而不是打印我的消息,它打印“Hello World”。还应该注意的是,我正在学习本教程:

http://www.raywenderlich.com/78574/android-tutorial-for-beginners-part-1#comments

在他们的教程中有一条声明说要将hello world字符串更改为我自己的自定义消息。问题是当我打开文件时没有hello world字符串。 (但是应用程序打印了hello world。)当我按照自己的方式在每个例子中发表一个hello world语句。

在我修改文件之前:

  <resources>
   <string name="app_name">OMG Android</string>
   <string name="action_settings">Settings</string>
 </resources>

修改文件后:

 <resources>
   <string name="app_name">OMG Android</string>
   <string name="action_settings">Settings</string>
   <string name="Hello_World">Dpolaristar is programming in Android</string>
 </resources>

该应用程序无需个人消息即可打印Hello World。我很困惑,任何人都可以告诉我,如果有某种自动覆盖或项目中的其他地方,它说能够打印我可以覆盖的Hello World。我已经看了,但找不到任何东西。

我的问题似乎也很奇怪,这让我觉得我要么碰到了我不应该拥有的东西,要么就是我使用的是不同版本的Android工作室,然后是本教程的作者。

帮助?

编辑:

我设法进入ac​​tivity_main.xml的文本视图以添加引用。问题是没有要添加的textview部分。

以下是我不会更改以供参考的文件的复制和粘贴:

     <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

   <android.support.design.widget.AppBarLayout   android:layout_height="wrap_content"
    android:layout_width="match_parent"

机器人:主题= “@风格/ AppTheme.AppBarOverlay” &GT;

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"  app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton  android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>       
编辑2:我查看了我的项目并发现在content_main.xml的DESIGN视图中,我确实显示了当我在MY TABLET上运行应用程序时输入的字符串的值!

这会给任何人提供任何线索吗?

4 个答案:

答案 0 :(得分:3)

根据您所遵循的教程,看起来好像您只生成一个空白布局资源文件,默认情况下会填充静态值“Hello World”。为了使用您创建的String资源操作此值,您需要调整存储Textview的布局文件。

enter image description here

现在根据您的代码,您需要编辑Layout文件的 android:text 部分。我生成了一个非常类似于你的字符串,但是我编辑了上面的资源文件以正确设置文本。现在,您可以看到使用我实现的自定义字符串更新的值。

enter image description here

enter image description here

每当您从Strings.xml文件引用String资源时,您将使用以下语法: android:text =“@ string / STRINGNAME”。这告诉IDE您从Strings.xml资源文件中提取字符串,并且您不想要输入的确切文本。

修改

好了添加了你的布局XML,你澄清了一堆:)我可以看到你生成了一个使用协调器布局配置的空白活动。应使用标记为 content_main.xml 的自动生成辅助布局。这是您需要查找TextView的地方。您正在查看主要布局,但不是单元内的容器。

enter image description here

答案 1 :(得分:2)

虽然链接中的教程有很多信息,但它没有很好的组织/结构。

为了展示“Hello World!”文本,您应该将其粘贴到您的活动的xml中(MainActivity - 可能):

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

如果你真的想用“Hello World!”作为字符串资源,只需在strings.xml中添加

<string name="text_4_my_hello_world_example">Hello World!</string>

并更改以下

上方的代码
android:text="@string/text_4_my_hello_world_example"

祝你好运,学习愉快!

答案 2 :(得分:1)

因为在您的布局文件中,TextView的文本属性必须如下所示:

android:text="@string/hello_world"

如果在strings.xml中创建一个新字符串:

<string name="my_string">My Beautiful String</string>

并在TextView中调用它:

<TextView
 ...
 android:text="@string/my_string"
 ... />

这应该有效。

答案 3 :(得分:1)

首先,属性名称必须为“hello_world”,不要使用大写字符,否则会导致您的项目出现问题!

<string name="hello_world">Darryl is learning Android!</string>

然后只需添加引用:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

然后,您会看到Darryl is learning Android!中显示的句子TextView

<string name="hello_world">Darryl is learning Android!</string> 必须在 /res/values/strings.xml

中设置

要打开包含TextView的布局,请转到 /res/layout/activity_main.xml /res/layout/activity_my.xml ,例如你的榜样。