从零开始学android:Activity初步
Activity
Activity其實就相當于CSS樣式,Activity實際上就是表示的是一個人機的交互程序,用于存放各個顯示控件,也是Android的基本組成,所有的Android項目都使用Java語言進行開發,所以每一個繼承了android.app.Activity的Java類都將成為一個Activity程序,而一個Android項目將由多個Activity程序所組成,而所有的顯示組件都必須放在Activity上才可以進行顯示,android.app.Activity類的繼承結構如下:
java.lang.Object
?? ??? ?android.content.Context
?? ? ?? ??? ?android.content.ContextWrapper
?? ??? ? ?? ??? ?android.view.ContextThemeWrapper
?? ? ?? ? ?? ? ?? ??? ?android.app.Activity
Activity類的常用方法
Android項目中的文件夾作用
Android項目中的文件作用
開發Android程序
打開文件res/layout/activity_main.xml(以后系統都默認打開的是這個文件)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello_world" /><TextViewandroid:id="@+id/mytext"android:layout_width="fill_parent"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/mybut"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </LinearLayout>
定義布局管理器,并增加組件
打開默認創建的文件:src/com.example.firstgingerbread/MainActivity.java
package com.example.firstgingerbread;import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); //調用父類的onCreate()方法LinearLayout layout = new LinearLayout(this); //定義布局管理器layout.setOrientation(LinearLayout.VERTICAL); //垂直 擺放所有組建TextView text = new TextView(this); //創建文本顯示組建text.setText(super.getString(R.string.message)); //從資源文件中設置文字Button but = new Button(this);but.setText(R.string.buttonstring);layout.addView(text);layout.addView(but);super.setContentView(layout); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
編輯values\strings.xml文件?
<?xml version="1.0" encoding="utf-8"?> <resources><string name="app_name">firstGingerbread</string><string name="action_settings">Settings</string><string name="hello_world">Hello world!</string><string name="message">Welcom to my world!</string><string name="buttonstring">按鈕</string> </resources>
小結
Android項目由若干個Activity程序所組成,每一個Activity都是一個Java類;
一個Android項目中所有用到的資源都保存在res文件夾之中;
Android中的組件需要在布局管理器中進行配置,之后在Activity程序中可以使用findViewById()方法查找并進行控制;
在布局管理器中定義的每一個組件都有其對應的操作類,用戶可以直接實例化這些類的對象進行組件的定義顯示;
標準的Android項目,所有的文字顯示信息應該保存在strings.xml文件中保存。
總結
以上是生活随笔為你收集整理的从零开始学android:Activity初步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql_install_db卸载_M
- 下一篇: 树莓派Raspberry 操作GPIO-