android - 具有两行并右对齐的 Android 表格布局问题

标签 android android-layout android-tablelayout

我在使用 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 布局总是缩进标签(它不跨越列),并且从不右对齐第二行中的任何内容。我不知道我做错了什么。这是Android 2.1。有人知道吗?

最佳答案

好的,我的回答如下:

<?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,这是用于密度独立像素(参见 What is the difference between "px", "dp", "dip" and "sp" on Android?)
  • 不要为最终产品使用内联颜色,将它们放在 res/values/colors 中,但我相信你知道这一点
  • 重命名我添加的 ID,同时删除“android:text”和“android:src”属性,因为它们只是为了向您展示。

关于android - 具有两行并右对齐的 Android 表格布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9165132/

相关文章:

android - 如何为文件浏览器应用程序 "myfiles"注册备用 mp3 查看器?

android - 使布局可滚动

java - 传递包含两个整数的对象或不同的方法?

android - inflater 是否需要 Activity 的上下文?

android - 让 child 的高度更新他 sibling 的高度

android - 表布局中的单选组,单选按钮与列对齐

android - TextViews 的某些部分不显示并被屏幕边缘截断

android - 触发系统音量条

java - fragment 中的 super.onCreateView

java - 更改 Android TableLayout 列跨度