【问题标题】:EXTREMELY SIMPLE android layout极其简单的安卓布局
【发布时间】:2011-04-24 11:22:38
【问题描述】:

我知道这很简单。但这让我很疯狂。我已经尝试了很多废话来让它看起来不错,但没有任何效果! :/

这是我的问题:

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

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

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

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

等等

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

【问题讨论】:

  • 如果您有很多动态行,您绝对应该使用带有自定义适配器的自定义列表视图项。

标签: android layout imageview tablelayout android-relativelayout


【解决方案1】:

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

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

[EDIT] ...或者正如 bigstones 在他的评论中所说 - 您可以使用带有自定义适配器的 ListView 而不是 table,这可能是更好的解决方案 :)

【讨论】:

    【解决方案2】:

    这就是我的工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多