非常简单的android布局

问题描述:

我知道这很简单。虽然这让我发疯。我已经尝试过这么多废话让这看起来很好,没有什么工作! :/非常简单的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

好了,我不明白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

这是我做的。

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; 
    } 
}