【问题标题】:Designing layout in Android with TableLayout and Gallery使用 TableLayout 和 Gallery 在 Android 中设计布局
【发布时间】:2011-02-15 12:13:16
【问题描述】:

我的应用中有两个ImageViews、一个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>

【问题讨论】:

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


    【解决方案1】:

    类似的东西。如果你想为 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>
    

    【讨论】:

      【解决方案2】:

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

      android:layout_weight="1"
      

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

      【讨论】:

        【解决方案3】:

        将此添加到您的 TableLayout-tag 中:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-03
          • 2012-05-27
          相关资源
          最近更新 更多