Android布局xml的include

來源:互聯網
上載者:User

Android布局xml的include
在一個項目中我們可能會需要用到相同的布局設計,如果都寫在一個xml檔案中,代碼顯得很冗餘,並且可讀性也很差,所以我們可以把相同布局的代碼單獨寫成一個模組,然後用到的時候可以通過 標籤來重用layout代碼。


btn.xml:

  1. android:layout_width="fill_parent"
  2. android:layout_height="wrap_content"
  3. android:orientation="vertical" >
    • android:id="@+id/btn"
    • android:layout_width="wrap_content"
    • android:layout_height="wrap_content"
    • android:text="Button">
    •  
  4. 複製代碼 main.xml
    1. android:layout_width="fill_parent"
    2. android:layout_height="fill_parent"
    3. android:orientation="vertical"
    4. >
    5. android:layout_width="fill_parent"
    6. android:layout_height="wrap_content"
    7. android:text="@string/hello" />
    8. 複製代碼 TestActivity:
      1. package com.hilary;
      2.  
      3. import android.app.Activity;
      4. import android.graphics.Color;
      5. import android.os.Bundle;
      6. import android.view.View;
      7. import android.view.View.OnClickListener;
      8. import android.widget.Button;
      9. import android.widget.LinearLayout;
      10. import android.widget.TextView;
      11.  
      12. import com.hialry.R;
      13. /**
      14. *
      15. *@author:hilary
      16. *@Date:2011-12-8
      17. *@description:
      18. *
      19. **/
      20. public class TestActivity extends Activity {
      21. private TextView tv = null;
      22. private LinearLayout ll = null;
      23. private LinearLayout ll2 = null;
      24.  
      25. /** Called when the activity is first created. */
      26. @Override
      27. public void onCreate(Bundle savedInstanceState) {
      28. super.onCreate(savedInstanceState);
      29. setContentView(R.layout.main);
      30. tv = (TextView) findViewById(R.id.tv);
      31. //如果一個布局檔案中包含同一個xml檔案,這兩個xml中的控制項Id是一樣的,當需要操作這些控制項時,需要通過定義這兩個View來加以區分,
      32. //如果就包含同一個xml檔案側不需要此步操作
      33. ll = (LinearLayout) findViewById(R.id.in1);
      34. ll2 = (LinearLayout) findViewById(R.id.in2);
      35.  
      36. ll.setBackgroundColor(Color.RED);
      37.  
      38. Button btn = (Button) ll.findViewById(R.id.btn);
      39. btn.setOnClickListener(new OnClickListener() {
      40.  
      41. @Override
      42. public void onClick(View v) {
      43. tv.setText("My name is hilary");
      44. }
      45. });
      46.  
      47. Button btn2 = (Button) ll2.findViewById(R.id.btn);
      48. btn2.setOnClickListener(new OnClickListener() {
      49.  
      50. @Override
      51. public void onClick(View v) {
      52. tv.setText(" You select second Button!");
      53.  
      54. }
      55. });
      56. }
      57. } 複製代碼 如果沒有include標籤,所有布局代碼都寫在一個xml檔案中,介面會顯得很冗餘,可讀性很差。而且介面載入的時候是按照順序載入的,前面的布局不能調用其後面的布局id。而採用include後,一個include中可以引用其後的include中的布局id屬性 -->

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.