【问题标题】:Android: can't remove vertical gap between images in LinearLayoutAndroid:无法删除 LinearLayout 中图像之间的垂直间隙
【发布时间】:2014-04-15 15:03:20
【问题描述】:

我在 Android 中有一个简单的 LinearLayout,有两个垂直图像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.eataly.android" 
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_header1"
        />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_header2"
        />
</LinearLayout>

正如您在以下链接中看到的那样,我无法消除图像顶部和底部的间隙: http://img185.imageshack.us/img185/8484/senzanomev.png

我已经在 LinearLayout 和 ImageViews 上尝试了所有可以想象的方法,即:

android:padding="0px"
android:top="0px"
android:bottom="0px"
android:top="0px"
android:layout_margin="0px"

没有成功。 我该怎么做才能删除这些空白边框?

【问题讨论】:

  • 您是否尝试过 layout_marginTop 的负值?
  • 哎呀,刚刚编辑了我的评论:它确实有效,但我不确定要设置哪个值(以 dp 为单位)。我应该尝试直到它们对齐吗?
  • 我遇到了类似的问题,我不得不尝试直到找到合适的值。如果有人可以提出更好的方法 - 非常欢迎!

标签: android imageview android-linearlayout


【解决方案1】:

我找到了解决方案。我需要将此属性添加到ImageView:

android:adjustViewBounds="true"

【讨论】:

    【解决方案2】:

    在 xml 布局中添加 android:scaleType="fitXY"。

    【讨论】:

    • 谢谢,我有 android:adjustViewBounds="true" android:scaleType="fitEnd" 并将其更改为“fitXY”。
    【解决方案3】:

    在线性布局中执行此操作
    对于顶部 ImageView:android:gravity="bottom"
    对于底部 ImageView:android:gravity="top"
    你确定你不想使用RelativeLayout吗?如果您要尝试将两个图像相互对齐,那么这似乎是未来的方法,恕我直言。

    【讨论】:

    • RelativeLayout 似乎没有解决问题:ImageView 似乎比它应该的高,所以 LinearLayout 和 RelativeLayout 的执行方式相同。实际上上面的建议(为layout_marginTop设置负值)似乎可行,所以它一定是ImageView的问题,而不是布局问题。
    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多