【问题标题】:How to rowspans and vertical alignment?如何行跨度和垂直对齐?
【发布时间】:2011-11-19 22:36:41
【问题描述】:

我正在尝试制作具有 2 列的行布局,就像这张图片中的 http://oi56.tinypic.com/1z2g9k9.jpg

总结

目标是有 2 列。右列的行跨度为 2,因此里面的图标中间垂直对齐左列的 2 行文本。

试试

到目前为止,我所拥有的是一个线性布局,其中包含一个相对布局(对齐两个文本行)和一个用于图标的 ImageView:

<?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="fill_parent"> 
    <RelativeLayout
        android:id="@+id/relativeLayoutLeft"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="5dip">
        <TextView
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_width="wrap_content"
            android:id="@+id/BigText" 
            android:layout_marginLeft="2dip">
        </TextView> 
        <TextView
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_width="wrap_content"
            android:id="@+id/SmallText" 
            android:layout_below="@+id/BigText"
            android:layout_marginLeft="2dip">
        </TextView>

    </RelativeLayout>
    <ImageView
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:id="@+id/Icon"
        android:src="@drawable/icon"
        android:layout_gravity="right"
        android:scaleType="center">
    </ImageView>  
</LinearLayout>        

问题

问题是RelativeLayout 有100% 的宽度并且图像在屏幕之外。 如果从fill_parent改为wrap_content,则图像不对齐到屏幕的右边缘(因为左列宽度并不总是90%左右)。

【问题讨论】:

    标签: android layout vertical-alignment html-table


    【解决方案1】:

    您应该能够将图像视图放置在 RelativeLayout 中,并像这样使用 alignParentRight 和 centerVertical。 alignParentRight 会将图像发送到相对(您的行)的右侧,而 centerVertical 会将其居中。

        <?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="fill_parent"> 
        <RelativeLayout
            android:id="@+id/relativeLayoutLeft"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:padding="5dip">
            <TextView
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:layout_width="wrap_content"
                android:id="@+id/BigText" 
                android:layout_marginLeft="2dip">
            </TextView> 
            <TextView
                android:layout_height="wrap_content"
                android:text="TextView"
                android:layout_width="wrap_content"
                android:id="@+id/SmallText" 
                android:layout_below="@+id/BigText"
                android:layout_marginLeft="2dip">
            </TextView>
            <ImageView
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:id="@+id/Icon"
                android:src="@drawable/icon"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:scaleType="center">
            </ImageView>  
    
        </RelativeLayout>
    </LinearLayout>   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 2017-06-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2015-07-15
      相关资源
      最近更新 更多