2014-03-05 26 views
2

菜鳥Android開發商在這裏尋找澄清一下:Android的初學者需要一些知識

顯示在屏幕上的信息,創建一個TextView小部件和設置使用的setText文本()。然後將TextView作爲Activity佈局的根視圖,並將它傳遞給setContentView()

具體是什麼意思是set the textroot view是什麼意思?謝謝。

+0

From here:https://developer.android.com/training/basics/firstapp/index.html?hl=cn –

回答

0

如果您是一個完整的newby,那麼可能「將TextView添加爲活動佈局的根視圖「(不管它的意思)是不是開始的正確的地方。試着通過學習你的第一個項目結構來理解基礎知識。假設你在Eclipse中工作,並沒有改變默認的活動(MainActivity.java)和佈局圖文件(activity_main.xml中)創建項目時,名稱,具體操作如下:

  • 轉到RES /佈局/ activity_main.xml中和下面的行添加到TextView的定義:

    機器人:ID = 「@ + ID/TV」

所以,你activity_main.xml中應該看起來像

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" > 

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

</RelativeLayout> 
  • 轉到SRC/your.package.name.MainActivity.java並添加行:

    TextView的TV =(TextView的)findViewById(R.id.tv);

    電視。setText(「你的文字」);

如果TextView以紅色突出顯示,請按Ctrl + Shift + O(Windows)導入所需的類。您應該看到:

package your.package.name; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TextView tv = (TextView) findViewById(R.id.tv); 
    tv.setText("Your text"); 
} 

} 
  • 運行你的應用程序。
1

設置

裝置設置顯示在TextView

根視圖

意味着非常基地Activity的佈局的View的文本的文本

Activity是由 View構建的,所以 Activity是可見的,根意味着佈局xml文件中的第一個標籤。

+0

... import android.widget.TextView; ... TextView setText = new TextView (這個); \t setText.setTextSize(40); \t setText.setText(message);喜歡這個? – user3381675

+0

靜態文本和文本大小通常在佈局xml文件中設置,但您也可以在Java代碼中動態設置它們。 – twlkyao

0
setText("Your Text") 

該函數設置其給出arguement到TextView的插件的文本(孩子)。其幾乎是像System.out.println(" ")System.out.print(" ") which displays the string to screen.But here setText(" ") displays to mobile screen.Actually the child(的TextView ) needs parent(Layout).So addView()`必須使用這個孩子添加到父。

setContentView() 

設置佈局資源的活動內容。該資源將被誇大,爲該活動添加所有頂級視圖。要詳細瞭解此操作,請點擊此link ..

+0

好吧,我已經導入了我的Textview窗口小部件。是這一行---> TextView textView = new TextView(this);我需要在setText中傳遞? – user3381675

+0

TextView textView = new TextView(this); //立即設置文本 textView.setText(「YourText」); //並將其添加到您的佈局。 yourLayout.addView(textView); –

+0

http://stackoverflow.com/questions/2300169/how-to-change-text-in-android-textview http://android4beginners.com/2013/06/lesson-1-3-how-to-修改-textview-in-java-code-findviewbyid -settext-and-gettext-methods/ 查看這些鏈接瞭解更多... –