1

我在使用 Android 布局时遇到问题。我要创建的内容类似于以下内容:

<table width="300px">
    <tr>
        <td span="2">test</td>
    </tr>
    <tr>
        <td align="right">image1</td>
        <td align="right">image2</td>
    </tr>
</table>

除了我希望“image1”和 2 在一起。无论如何,

这是我在 Android 中使用的布局:

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow android:layout_gravity="left" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_gravity="left|center_vertical"
            android:layout_span="2"
            android:paddingRight="5dip"
            android:textColor="#ffffffff"
            android:textSize="13dip" />
    </TableRow>

    <TableRow android:layout_gravity="right" >

        <ImageView
            android:layout_width="50px"
            android:layout_height="120px"
            android:layout_column="1"
            android:layout_marginLeft="15dip" />

        <ImageView
            android:layout_width="263px"
            android:layout_height="150px"
            android:layout_column="2"
            android:layout_marginLeft="15dip" />
    </TableRow>

</TableLayout>

我还尝试将 android:layout_gravity:"right" 放在 ImageViews 本身上,但什么也没做。

出于某种原因,这个 Android 布局总是缩进标签(它不跨越列),并且永远不会右对齐第二行中的任何内容。我不知道我做错了什么。这是安卓2.1。有人知道吗?

4

3 回答 3

2

好的,我的回答如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

          <TextView
                android:id="@+id/textid"
                android:text="SOME TEXT"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:paddingRight="5dip"
                android:textColor="@color/white"
                android:textSize="13dip" />

            <ImageView
                android:src="@drawable/cloud1"
                android:id="@+id/imageview1"
                android:layout_alignParentRight="true"
                android:layout_below="@id/textid"
                android:layout_width="50dp"
                android:layout_height="120dp"
                android:layout_marginLeft="15dp" />

            <ImageView
                android:src="@drawable/grey_bar"
                android:layout_alignParentRight="true"
                android:layout_below="@id/imageview1"
                android:layout_width="263dp"
                android:layout_height="150dp"
                android:layout_marginLeft="15dp" />

</RelativeLayout>

还有一些其他的指针:

  • 不要使用 PX 使用 dp,这是针对与密度无关的像素(请参阅Android 上的“px”、“dp”、“dip”和“sp”有什么区别?
  • 不要在最终产品中使用内联颜色,将它们放在 res/values/colors 中,但是我相信你知道这一点
  • 重命名我添加的 ID,同时删除“android:text”和“android:src”属性,因为它们被放入只是为了向您展示。
于 2012-02-06T18:44:02.213 回答
1

我最终用这种布局解决了这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="5dip"
        android:textColor="#ffffffff"
        android:textSize="13dip" />

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableRow android:gravity="right" >

            <ImageView
                android:layout_width="50px"
                android:layout_height="120px"
                android:layout_column="1" />

            <ImageView
                android:layout_width="263px"
                android:layout_height="150px"
                android:layout_column="2"
                android:paddingRight="20dip" />
        </TableRow>
    </TableLayout>

</LinearLayout>

另外,另一个问题是我在代码中按像素设置宽度/高度,这取决于我需要什么,当你这样做时,你也会清除列..所以代码必须是这样的:

        abc = new TableRow.LayoutParams(myImageWidth,
                mykpiImageHeight);
        abc.column = 2;
于 2012-02-07T18:26:42.620 回答
0

它已被确认为 ADT 中的一个错误,并被认为已在 ADT 21 中修复。请参阅已修复的错误 - 最后

于 2012-10-17T21:36:02.903 回答