Android開發相關:(01)完全利用代碼去構建用戶界面

java] package com.androidbook.controls; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
    private LinearLayout nameContainer; 
    private LinearLayout addressContainer; 
    private LinearLayout parentContainer; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        createNameContainer(); 
        createAddressContainer(); 
        createParentContainer(); 
 
        setContentView(parentContainer); 
    } 
 
    private void createNameContainer() { 
        nameContainer = new LinearLayout(this); 
        nameContainer.setLayoutParams(new LayoutParams( 
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
        nameContainer.setOrientation(LinearLayout.HORIZONTAL); 
 
        TextView nameLbl = new TextView(this); 
        nameLbl.setText("Name: "); 
 
        TextView nameValue = new TextView(this); 
        nameValue.setText("Hohn Doe"); 
 
        nameContainer.addView(nameLbl); 
        nameContainer.addView(nameValue); 
    } 
 
    private void createAddressContainer() { 
        addressContainer = new LinearLayout(this); 
        addressContainer.setLayoutParams(new LayoutParams( 
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
        addressContainer.setOrientation(LinearLayout.VERTICAL); 
 
        TextView addrLbl = new TextView(this); 
        addrLbl.setText("Address: "); 
 
        TextView addrValue = new TextView(this); 
        addrValue.setText("911 Hollywood Blvd"); 
 
        addressContainer.addView(addrLbl); 
        addressContainer.addView(addrValue); 
    } 
 
    private void createParentContainer() { 
        parentContainer = new LinearLayout(this); 
        parentContainer.setLayoutParams(new LayoutParams( 
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
        parentContainer.setOrientation(LinearLayout.HORIZONTAL); 
 
        parentContainer.addView(nameContainer); 
        parentContainer.addView(addressContainer); 
    } 

摘自 horsttnann的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *