极其简单的android布局

时间:2011-04-23 22:01:35

标签: android layout imageview tablelayout relativelayout

我知道这很简单。这让我很疯狂。我已经尝试了这么多废话让这看起来很好而且没有工作! :/

这是我的问题:

我希望布局看起来像这样:

固定标题,其高度与放在左侧的图像相同。在图像的右侧,我只想要简单的文本,可以跨越2行。

在固定标题下面,我需要某种表布局,如:

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

在这些行的任何一行中,文本可能跨越2行,具体取决于数据库中的内容。第一列中的文本全部是静态的,而第二列中的文本是动态的。表行需要放入ScrollView(简单部分),以便它可以保存大量信息。我尝试过使用TableLayout,它给了我各种令人头疼的问题。同样对于固定标题,我使用layout_below及以上的整个RelativeLayout事项来使其工作,但我无法获得图像间距和正确的文本。真是太头疼了!谢谢你的帮助!

2 个答案:

答案 0 :(得分:3)

好吧,我不明白它应该看起来如何,但请记住你可以嵌套布局,所以你可以使用像

这样的东西
<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>

如果您提供有关您遇到的问题的更多信息,也许我可以提供更多帮助。

[编辑] ...或者正如大石头在他的评论中所说 - 而不是桌子,你可以使用ListView和自定义适配器,这可能是更好的解决方案:)

答案 1 :(得分: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;
  }
}