Android中的Linerlayout佈局和RelativeLayout佈局,ListView組件

佈局

1. LinerLayout:線性佈局:LinearLayout是線性佈局控件,它包含的子控件將以橫向或豎向的方式排列,可以再一個線性佈局裏面加入相對佈局。Android:orientation:設置它內容的對其方向(橫向/豎向),支持widget權重。

屬性:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <span style="white-space:pre">    </span>  android:layout_width="fill_parent"  
  2.    <span style="white-space:pre"> </span>  android:layout_height="fill_parent"  
  3.   <span style="white-space:pre">  </span>  android:orientation="horizontal"  
  4.   <span style="white-space:pre">  </span>  android:padding="10dip"  
  5.           android:id="@+id/ll_item"  

 

2.RelativityLayout(相對佈局):可以設置組件和組件的相對關係,通過layout_XX方法實現。                                                                                                          

RelativeLayout用到的一些重要的屬性:


第一類:屬性值爲true或false
android:layout_centerHrizontal                                       水平居中
android:layout_centerVertical                                         垂直居中
android:layout_centerInparent                                       相對於父元素完全居中
android:layout_alignParentBottom                   貼緊父元素的下邊緣
android:layout_alignParentLeft                      貼緊父元素的左邊緣
android:layout_alignParentRight                     貼緊父元素的右邊緣
android:layout_alignParentTop                      貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing              如果對應的兄弟元素找不到的話就以父元素做參照物

第二類:屬性值必須爲id的引用名“@id/id-name”
android:layout_below                        在某元素的下方
android:layout_above                        在某元素的的上方
android:layout_toLeftOf                     在某元素的左邊
android:layout_toRightOf                    在某元素的右邊

android:layout_alignTop                     本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft                     本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom                本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight                   本元素的右邊緣和某元素的的右邊緣對齊

第三類:屬性值爲具體的像素值,如30dip,40px
android:layout_marginBottom             離某元素底邊緣的距離
android:layout_marginLeft                  離某元素左邊緣的距離
android:layout_marginRight                離某元素右邊緣的距離
android:layout_marginTop                  離某元素上邊緣的距離



ListView:最初的印象是使用它就可以不用在一組textView上包上ScrollView了,實現滑動展示數據。

使用方法,先findViewByid()獲得這個listbiew--> lv;然後調用 lv.setAdapter(Adapter)設置適配器,這樣,這個listview中展時,會調用這個適配器的方法,實現數據展示,關於適配器,晚上回來繼續寫,先睡會去上課。。

-----

繼續分析listview,昨天說要set一個適配器,充當視圖和數據之間的橋樑作用,mvc中的v和c,這個適配器要繼承一個BaseAdapter,裏面有四個方法,暫時用到的只有兩個,getCount(),getView(int position, View convertView, ViewGroup parent);position是要提取的對象id,根據getCount的總數和屏幕位置,會連續間斷調用這個方法,從0--count-1;converView是已經緩存的不在屏幕上顯示的對象。還有一個類方法,

flater = MainActivity.this.getLayoutInflater();
view = flater.inflate(R.layout.person_item,null);

獲得一個佈局填充器,然後把一個佈局文件變成一個view,getView()方法返回的就是一個xml佈局的view形式。


[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. package com.test.database;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.test.database.R.id;  
  6. import com.test.database.dao.PersonDao;  
  7. import com.test.database.model.Person;  
  8.   
  9. import android.support.v4.app.Fragment;  
  10. import android.app.Activity;  
  11. import android.os.Bundle;  
  12. import android.view.LayoutInflater;  
  13. import android.view.Menu;  
  14. import android.view.MenuItem;  
  15. import android.view.View;  
  16. import android.view.ViewGroup;  
  17. import android.widget.BaseAdapter;  
  18. import android.widget.LinearLayout;  
  19. import android.widget.ListView;  
  20. import android.widget.TextView;  
  21. import android.os.Build;  
  22.   
  23. public class MainActivity extends Activity {  
  24.     List<Person> persons ;  
  25.     Person person1 ;  
  26.           
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.           
  32.          
  33.           
  34.         ListView lv = (ListView)findViewById(R.id.lv);  
  35.           
  36.         lv.setAdapter(new MyAdapter());  
  37.       
  38.     }  
  39.       
  40.     class MyAdapter extends BaseAdapter {  
  41.   
  42.         @Override  
  43.         public int getCount() {  
  44.             // TODO Auto-generated method stub  
  45.             return 1;  
  46.         }  
  47.   
  48.         @Override  
  49.         public Object getItem(int arg0) {  
  50.             // TODO Auto-generated method stub  
  51.             return null;  
  52.         }  
  53.   
  54.         @Override  
  55.         public long getItemId(int arg0) {  
  56.             // TODO Auto-generated method stub  
  57.             return 0;  
  58.         }  
  59.   
  60.         @Override  
  61.         public View getView(int position, View convertView, ViewGroup parent) {  
  62.             LayoutInflater flater = null;  
  63.             View view = null;  
  64.               
  65.               
  66.             if(convertView == null) {  
  67.                 // 佈局填充器對象, 用於把xml佈局轉換成view對象  
  68.                 flater = MainActivity.this.getLayoutInflater();  
  69.                 view = flater.inflate(R.layout.person_item,null);  
  70.             } else {  
  71.                 view = convertView;  
  72.             }  
  73.               
  74.                       
  75.               
  76.             TextView tv_name = (TextView)view.findViewById(R.id.tv_name);  
  77.             TextView tv_age = (TextView)view.findViewById(R.id.tv_age);  
  78.               
  79.             tv_name.setText("11z");  
  80.             tv_age.setText("22");  
  81.               
  82.             return view;                  
  83.         }  
  84.     }  
  85. }  

這是自己寫適配器的例子,還可以用比較常用方便的android提供的適配器,


ArrayAdapter(Context context,View view,String[] string)

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
         ArrayListDemo.this, android.R.layout.simple_list_item_1,
         adapterData);

這段代碼是創建一個數組適配器的代碼,裏面有三個參數,第一個參數是上下文,就是當前的Activity, 第二個參數是android sdk中自己內置的一個佈局,它裏面只有一個TextView,這個參數是表明我們數組中每一條數據的佈局是這個view,就是將每一條數據都顯示在這個view上面;第三個參數就是我們要顯示的數據。listView會根據這三個參數,遍歷adapterData裏面的每一條數據,讀出一條,顯示到第二個參數對應的佈局中。


SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)


代碼片段:

Map<String ,object> person = new HashMap<String, object>()

List<Map<String,object>> list = new ArrayList<Map<String,object>>

適配器的context就是上下文,data就是這個map集合list,resource是XML佈局文件的資源ID,from數組是Map對象的String項,就是名稱,然後to是指這個佈局文件中的每個組件的資源ID,這樣這個適配器就會遍歷每一個list中的對象,然後把所有的map按照名稱把對應的value數值放到佈局文件對應的組件中,進行顯示。

發佈了15 篇原創文章 · 獲贊 32 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章