1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

APP 基本架構

Last updated at Posted at 2019-07-29

佈局

範例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center" 
    xmlns:android="http://schemas.android.com/apk/res/android" >
 
     將要放的佈局物件放在LinearLayout裡
     .
     .
     .
 
</LinearLayout>



* **layout_height** 與 **android:layout_width** 是基本設定,如果不宣告便會出錯。
* **orientation** 用來設定排列方
* **gravity** 用來放置的位置(置中、置左...)

### 佈局物件範例

```xml

    <EditText
        android:id="@+id/et_name"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_enter"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="push it"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/tv_s1"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="heheXD"
        android:textSize="20dp"/>
  • EditText : 可編輯文字方塊
  • Button : 按鈕
  • TextView : 文字
  • id 是用來宣告這個物件的名稱,可在主程式內呼叫與操作。

MainActivity

從畫面得到物件,並轉換成該物件型態


public class MainActivity extends AppCompatActivity {
    private EditText et_name;
 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//該畫面中的布局
        et_name = (EditText)findViewById(R.id.et_name);//物件型態轉換
        
        Toast.makeText(MainActivity.this,"successful",Toast.LENGTH_LONG).show();//產生成功提示
    }
}

 

TAG 用法

  • 可以用來顯示於debug視窗,方便偵錯。

private final String TAG = MainActivity.class.getSimpleName();

Log.i(TAG, put some to debug );

建立物件

  • 點選右鍵 app->java->new->java class

  • 編輯畫面中右鍵使用generate 幫助跨快速建構function 如: 建構子 set & get

ListView 使用

Android 學習筆記 - 建立簡單的 ListView

參考資料

透過setContentView轉換layout
Android Context完全解析
Android入門基礎:從這裡開始
Android 學習筆記 - 建立簡單的 ListView

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?