android學習筆記—51_編碼實現軟體介面,把固定不變的介面寫到xml中,邏輯改變的寫到程式中,

來源:互聯網
上載者:User

2013/5/12
51_編碼實現軟體介面
--------------------------
1.Android除了可以使用xml實現軟體介面,還可以通過編碼方式實現軟體的介面,而且在某種情況下只能採用編碼方式實現軟體的介面,例如:軟體運行時需要根據運算結果決定顯示某些內容。如果不是必須,建議使用xml,
  因為這樣可以使應用遵守mvc設計模式,具有良好的軟體分層結構。
-----------------------------------------------------------------
2.根據計算結果顯示樣式的時候,這時候使用xml做介面就很難
------------------------------------
3.比如:
   ----------------------------------------
   這個部分經常改變,可以放到編碼中來控制
   ----------------------------------------
   --------------------------------------------
   這部分不變,不變的部分可以抽取出來放到xml中。
   ----------------------------------------------
   a.如果通過編碼實現軟體介面的時候,要把不變的部分放到xml中,這樣以後修改介面的時候比較方便
   b.但通過編碼方式實現介面效能上要更好一些
   c.軟體要遵守mvc模式
 ------------------------------------------------------
 4.下面是通過編碼實現介面的執行個體源碼
   a.建立android項目:CodeUI
   b./CodeUI/src/com/credream/codeUI/CodeUIActivity.java
    package com.credream.codeUI;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CodeUIActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       //①. 建立一個線性布局對象,傳入內容物件
        LinearLayout linearLayout = new LinearLayout(this);
        /*LinearLayout的父類是ViewGroup
         * ViewGroup是一個view容器:用來裝一些子控制項,只有繼承ViewGroup才有
         * 容納子控制項的功能,ViewGroup又繼承了View,在android中,不管是控制項還是
         * 容器,最後都繼承自View類
         *
         * *
         */
        //垂直布局
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        //①.1 建立TextView
        TextView textView = new TextView(this);
       //設定TextView顯示內容
        textView.setText(R.string.hello);
       //指定textView的布局參數:寬高什麼的。
        //這裡指定寬填充父元素,高WRAP_CONTENT包裹內容
        ViewGroup.LayoutParams textViewParams = new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //①.2把textView控制項添加到線性布局中。textView布局控制項,textViewParams全域參數
        linearLayout.addView(textView, textViewParams);
     // ③ 得到part.xml中的布局view
        View partView = getPartUI();
        //③.1 把part.xml部分的布局添加到線性布局中
        linearLayout.addView(partView);
       
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        setContentView(linearLayout, layoutParams);
    }
    //② 用來得到某個部分的介面
    private View getPartUI(){
     //這裡用到了布局填充服務LayoutInflater,是android系統內建的服務
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     return inflater.inflate(R.layout.part, null);
     //inflater.inflate(R.layout.part, null);返回View,
     //第一個參數是資源ID,可以根據介面ID這裡是part.xml中,產生對應的View對象
     //第二個參數是:指定是否把產生的view對象掛接到某個父元素上面
    }
}
------------------------------------------------------------------------------------
c./CodeUI/res/layout/part.xml
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<!-- 這個xml是控制的介面中不變的部分 ,這裡添加了一個EditText控制項-->
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
        android:text="添加"    
        />
</LinearLayout>
------------------------------------------------
d./CodeUI/res/values/strings.xml
  <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, CodeUIActivity!</string>
    <string name="app_name">CodeUI編碼實現軟體介面</string>

</resources>
------------------------------------------------------------------------

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.