2011-04-23 240 views
0

我知道這很簡單。雖然這讓我發瘋。我已經嘗試過這麼多廢話讓這看起來很好,沒有什麼工作! :/非常簡單的android佈局

這裏是我的問題:

我希望有一個佈局,看起來像這樣:

固定頭,其高度相同被放在左邊的圖像。在圖像的右側,我只想簡單的文字,可以跨越2行。

固定頭下面,我需要某種表格佈局的,如:

---------------------------------- 
Birth date  |  09/23/1945 
---------------------------------- 
Death date  |  04/27/2011 
---------------------------------- 

在這些行中的任何一個,文本可能跨越兩行根據什麼是在數據庫。第一列中的文本全部是靜態的,而第二列中的文本是動態的。表格行需要放入ScrollView(簡單部分),因此它可以容納大量信息。我嘗試過使用TableLayout,它一直給我帶來各種令人頭疼的問題。同樣對於固定標題,我使用了與layout_below及以上版本相關的整個RelativeLayout工作,但無法使圖像間距和文本正確顯示。這很頭疼!謝謝你的幫助!

+1

你一定要使用自定義列表視圖項與自定義適配器,如果你有很多的動態行。 – bigstones 2011-04-23 22:29:21

回答

3

好了,我不明白100%它應該如何看,但要記住,你可以窩佈局,這樣你就可以使用像

<LinearLayout 
    orientation="vertical" 
    width="match_parent" 
    height="wrap_content"> 

    <!-- header --> 
    <LinearLayout 
     orientation="horizontal" 
     width="match_parent" 
     height="wrap_content"> 

     <ImageView 
      width="wrap_content" 
      height="wrap_content"> 
     <TextView 
      width="match_parent" 
      height="wrap_content" 
      lines="2"> 
    </LinearLayout> 

    <!-- Table here. You can use either TableLayout or nested LinearLayouts. 
     I prefer LinearLayouts. They have also a nice feature: 
     <LinearLayout> 
      <View width="match_parent" weight="1" /> 
      <View width="match_parent" weight="1" /> 
     </LinearLayout> 
     ...makes both views equally wide, which you can use in your table. --> 
</LinearLayout> 

也許我可以幫助更多的,如果你給多一點有關您遇到的問題的信息。

[編輯] ...或bigstones說,在他的評論 - 而不是表,你可以使用的ListView使用自定義適配器,這可能是更好的解決方案:)

+0

非常感謝你.....我一直在努力! :) – SpellChucker 2011-04-24 02:48:52

1

這是我做的。

row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical"> 
    <TextView 
     android:id="@+id/left_text" 
     android:layout_width="75dip" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/right_text" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:maxLines="2" 
     android:ellipsize="end" /> 
</LinearLayout>

主要應用

public class Main extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    List<Datum> data = new ArrayList<Datum>(); 
    data.add(new Datum("FIRST", "One line of text.")); 
    data.add(new Datum("SECOND", "Two lines of text. Two lines of text. Two lines of text.")); 
    data.add(new Datum("THIRD", "Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text.")); 
    data.add(new Datum("FOURTH", "One line of text, again.")); 

    ListView leftList = (ListView) findViewById(R.id.my_list); 
    leftList.setAdapter(new MyAdapter(this, R.layout.row, data)); 
    } 
} 

class Datum 
{ 
    String left, right; 

    Datum(String left, String right) 
    { 
    this.left = left; 
    this.right = right; 
    } 
} 

class MyAdapter extends ArrayAdapter<Datum> 
{ 
    List<Datum> data; 
    int textViewResourceId; 

    MyAdapter(Context context, int textViewResourceId, List<Datum> data) 
    { 
    super(context, textViewResourceId, data); 
    this.data = data; 
    this.textViewResourceId = textViewResourceId; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    if (convertView == null) 
    { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(textViewResourceId, null); 
    } 
    Datum datum = data.get(position); 
    if (datum != null) 
    { 
     TextView leftView = (TextView) convertView.findViewById(R.id.left_text); 
     TextView rightView = (TextView) convertView.findViewById(R.id.right_text); 
     if (leftView != null) 
     { 
     leftView.setText(datum.left); 
     } 
     if (rightView != null) 
     { 
     rightView.setText(datum.right); 
     } 
    } 
    return convertView; 
    } 
}