极其简单的 Android 布局

发布于 2024-11-02 22:10:34 字数 611 浏览 0 评论 0原文

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

这是我的问题:

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

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

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

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

等。

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

I know this HAS to be simple. It's making me crazy though. I've tried so much crap to get this looking good and NOTHING IS WORKING!! :/

Here is my problem:

I want a layout to look like this:

A fixed header with a height the same as an image that is put on the left. To the right of the image, I want just simple text that has the possibility of spanning 2 lines.

Below the fixed header, I need some sort of table layout like:

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

etc.

In any one of those rows, the text may span 2 lines depending on what is in the database. The text in the first column is all static while the text on the second column is dynamic. The table rows need to be put into a ScrollView (easy part) so it can hold a lot of information. I've tried using a TableLayout and it has been giving me all sorts of headaches. Also for the fixed header, I used the whole RelativeLayout thing with layout_below and above to make that working but I can't get the image spacing and the text to the right correct. It's such a headache! Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-11-09 22:10:34

好吧,我并不 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,这可能是更好的解决方案:)

Well, I do not understand 100% how it's supposed to look, but remember that you can nest layouts, so you can use something like

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

Maybe I can help more if you give a bit more information about problems you're encountering.

[EDIT] ...or as bigstones says in his comment - instead of table you can use ListView with a custom adapter, that's probably much better solution :)

猫九 2024-11-09 22:10:34

这就是我所做的。

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

Here's what I do.

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>

Main App

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文