2013-10-02 34 views
1

我想要創建一個items列表。每個列表項應該有四個文本。而這四者都將放在該列表的不同四角。我可以在xml文件中做。但我無法弄清楚如何從Android中的java中執行此操作。因爲我需要從我的數據庫中創建這些列表。爲了更好地理解我的問題,下面是我的XML代碼。請問誰能幫我..?從android中的java格式化佈局

   <?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="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/tvb" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical|center_horizontal" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="left" 
      android:text="Left Aligned" 
      android:textSize="25dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="right" 
      android:text="Right Aligned" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical|center_horizontal" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="left" 
      android:text="Left Aligned" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="right" 
      android:text="Right Aligned" /> 
    </LinearLayout> 
</LinearLayout> 

+0

好吧!所以有什麼問題? –

+1

我建議使用單個RelativeLayout而不是像這樣深度嵌套的LinearLayout。閱讀起來會更容易,並且可能會提高應用程序的性能。 –

+0

非常感謝@ Tanis.7x。但是,如果可以給我一些示例java代碼來獲取該代碼將更有幫助:D – exponentialFun

回答

1

先看看這個:http://www.vogella.com/articles/AndroidListView/article.html

的綜合指南ListView S IN一般以及如何創建自己的自定義Adapter秒。

現在,當你看着這所有的樂趣;-)

getView方法被稱爲對每個你可能想看看你的自定義AdaptergetView方法,因爲這是ListView項目在您的列表中。

這是你膨脹你的XML佈局和設置你的佈局文件的文本。

的如何getView方法可能看起來像一個例子,可能是這樣的:

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if(convertView == null) { 
     convertView = inflater.inflate(R.layout.myrowlayout, parent, false); 
    } 

    TextView textView1 = (TextView) rowView.findViewById(R.id.textview1); 
    TextView textView2 = (TextView) rowView.findViewById(R.id.textview2); 
    TextView textView3 = (TextView) rowView.findViewById(R.id.textview3); 
    TextView textView4 = (TextView) rowView.findViewById(R.id.textview4); 

    // Set the text of the textViews accordingly to where you are in the list. 
    // An example 
    if(position % 2 == 0) { 
     // If even position in the list, set the first TextView 
     textView1.setText("This text is only shown for even positions in your list");    
    } 

    return rowView; 
} 

你也可以看看Collection你已經提供給擺在首位的Adapter,看看是否有什麼特殊項目,你想要應用特定的佈局。

這也可以應用於特定的屏幕尺寸 - 例如,平板電腦應該使用GridView而不是ListView(在大多數情況下)。

偏題:我的代碼示例絕不是高效的加載項目。如果你想要高效的代碼,你應該看看ViewHolder pattern

希望這可以幫助你一路:-)