【问题标题】:Android table layout image marginAndroid表格布局图片边距
【发布时间】:2012-07-11 14:15:04
【问题描述】:

我有以下表格布局,用于在网格中显示图像按钮。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TableRow
    android:id="@+id/tableRow4"
    android:layout_weight="1" >

    <ImageButton
        android:id="@+id/button_one"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/button"
        android:scaleType="fitCenter"
        android:src="@drawable/button_one" />

    <ImageButton
        android:id="@+id/button_two"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/button"
        android:scaleType="fitCenter"
        android:src="@drawable/button_two" />
</TableRow>

<TableRow
    android:id="@+id/tableRow4"
    android:layout_weight="1" >

    <ImageButton
        android:id="@+id/button_three"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="@drawable/button"
        android:scaleType="fitCenter"
        android:src="@drawable/button_three" />
</TableRow>

如您所见,第一行包含两列,第二行包含一列。

我希望图像展开以适合表格单元格。然而,底部图像按钮的边缘与顶部的两个图像按钮并没有完全对齐。就好像底部单元格上有额外的填充或边距。

任何想法如何让这些完美排列?

提前致谢。

截图:

【问题讨论】:

  • 尝试用fitXY替换所有fitCenter
  • @vasart 这并没有解决问题:(我认为这可能与 layout_weight 有关,因为删除会删除边距,但是按钮不会填满屏幕。

标签: android android-layout


【解决方案1】:

我解决了这个问题……

我最初使用九个补丁绘制作为背景,因为我有弯曲的角落,但是当我切换到带有尖角的常规图像时,我忘记了从图像中删除边框......哇!重做图像解决了问题 - 我的 xml 原样正常。

/掌脸

【讨论】:

    【解决方案2】:

    正如 vasart 所述,将所有 fitCenter 替换为 fitXY 即可解决问题

    更新的 XML:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff" >
    
        <TableRow
            android:id="@+id/tableRow4"
            android:layout_weight="1" >
    
            <ImageButton
                android:id="@+id/button_one"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:background="@drawable/button"
                android:scaleType="fitXY" />
    
            <ImageButton
                android:id="@+id/button_two"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:background="@drawable/button"
                android:scaleType="fitXY" />
        </TableRow>
    
        <TableRow
            android:id="@+id/tableRow5"
            android:layout_weight="1" >
    
            <ImageButton
                android:id="@+id/button_three"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:background="@drawable/button"
                android:scaleType="fitXY" />
        </TableRow>
    
    </TableLayout>
    

    【讨论】:

    • 谢谢,今晚我试试看。
    • 它不能解决问题。结果相同,只是内部图像现在失真了。
    • @TonyLeeper 请上传您在这种情况下使用的图片
    • 是的,除了背景之外,请参阅我的示例 xml 中的“android:src”。我删除了屏幕截图的图像,因为它们与问题无关,并且特定于我在 android 应用商店中拥有的应用,并且不想无意中将 SO 用作广告平台。我只是想让按钮正确排列。
    猜你喜欢
    • 2015-06-24
    • 2013-09-19
    • 2013-11-23
    • 2016-10-05
    • 2017-04-15
    • 1970-01-01
    • 2014-01-31
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多