Android的使用的setContentView TextView的 [英] Android setContentView with Textview

查看:169
本文介绍了Android的使用的setContentView TextView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是完全新的Andr​​oid应用程序,和我读通过谷歌的教程:的 http://developer.android.com/training/basics/firstapp/starting-activity.html

I'm completely new to Android application programming, and I was reading through Google's tutorial: http://developer.android.com/training/basics/firstapp/starting-activity.html.

在此页面中,显示的信息部分下,他们创造的TextView 对象,并使用的setContentView 的TextView 对象作为参数,显示一些文本。我在想,如果我理解正确,而不是code内创建的TextView 对象,你可以在XML定义它呢?如果您在XML定义它,会要求您创建除了main_activity.xml一个新的XML文件?谢谢你。

On this page, under the "Display the Message" section, they create TextView object, and use setContentView with the textView object as the argument, to display some text. I was wondering, if I'm understanding correctly, instead of creating the TextView object within the code, can you define it in XML instead? If you define it in XML, would that require you to create a new XML file besides main_activity.xml? Thanks.

推荐答案

您可以声明一个XML中所有的布局和视图。对于给定的例子中,code看起来像下面的

You can declare all your layouts and views inside a xml. For the given example, the code would look like the following

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set your parent view
    setContentView(R.layout.main_layout);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Get the reference to the TextView and update it's content
    TextView textView = (TextView)findViewById(R.id.my_text_view);
    textView.setText(message);
}

和您的main_layout.xml看起来像

And your main_layout.xml would look like

<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="40sp"
           android:id="@+id/my_text_view"/>

这篇关于Android的使用的setContentView TextView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆