2013年11月20日

Activityとは

Activity

簡単に言うと目に見えるアプリ画面です。

Activity

下記ソース実行例
画面にHello World!と記載されてます。
(文字ちっさ!)

①res/layout/activity_main.xmlに書かれた内容が表示される
②res/valuse/strings.xmlに記載している"Hello World!"とアプリ画面にテキストが表示される。










MainActivity.java
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);//①activity_main.xmlをセット
 }
}

res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />//②
</RelativeLayout>

res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HelloWorld</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>


0 件のコメント:

コメントを投稿