使用TableLayout和Gallery在Android中设计布局

时间:2011-02-15 12:13:17

标签: android android-layout layout tablelayout android-gallery

我的应用中有两个ImageView,一个TextView和一个Gallery,我希望我的布局如下所示。

............................................-.
<ImageView>|     <TextView>      |<ImageView>|
..............................................
|                                            |
|                  Gallery                   |
|                                            |
|............................................|

我有一个表格布局,几乎得到了如上所示的设计,只是当文本长度很小时,屏幕右端的ImageView继续移动。我想要固定屏幕左右两端的ImageView,无论文本大小如何。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

         <TableRow>
              <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
                   android:id="@+id/imageL"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"  
                   android:layout_weight="1"
                   android:minHeight="30px"
                   android:minWidth="30px" />

              <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                   android:id="@+id/title"
                   android:paddingLeft="10px"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" 
                   android:layout_weight="3"
                   android:layout_gravity="center"
                   android:minHeight="30px"
                   android:minWidth="30px" />

               <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
                   android:id="@+id/imageR"
                   android:paddingLeft="10px"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:layout_weight="1"
                   android:minHeight="30px"
                   android:minWidth="30px" />
          </TableRow> 
    </TableLayout>

    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/gallery"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" />
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

喜欢它的东西。如果你想为img制作静态宽度和高度你可以在android:layout_width和android:layout_height中设置它

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
    <ImageView 
    android:id="@+id/firstImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/icon"
    />
    <TextView 
    android:id="@+id/someText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/secondImage"
    android:layout_toRightOf="@+id/firstImage"
    android:text="some mega text."
    android:gravity="center_horizontal"
    />
    <ImageView 
    android:id="@+id/secondImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/icon"
    />

</RelativeLayout>

答案 1 :(得分:0)

对所有三个项目使用相同的权重,问题应该解决。

android:layout_weight="1"

所有3个将占据相同的区域。

答案 2 :(得分:0)

将此添加到您的TableLayout标记:

android:shrinkcolumns="0,2" android:stretchColumns="1"

此外,您可能希望将layout_width更改为fill_parent,因为这将允许中间列拉伸。