ImageView中不需要的填充或边距

时间:2017-02-21 09:48:42

标签: android android-layout android-studio

[![在此处输入图片描述] [1]] [1]我是android的android开发新手,只是玩xmls来构建布局。我编写了以下代码,用于打印hello ubuntu并在其中插入图像。但是Image似乎有一个不需要的上部和下部填充或边距(不确定它叫什么)看起来很奇怪。请任何人帮我删除它。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical"
    android:padding="10dp"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_margin="8dp"
        android:text="Hello Ubuntu (16.06LTS)"
        android:background="@color/colorPrimary"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:src="@mipmap/Ubuntu"
        android:layout_margin="0dp"

        />
    </LinearLayout>

此处的截图(请参见右侧的前面部分):https://i.stack.imgur.com/j9NlT.png

4 个答案:

答案 0 :(得分:3)

您应该使用android:scaleType(请参阅documentation)。 如果图像没有确切的视图大小,这将允许您告诉视图如何做出反应。

答案 1 :(得分:1)

在imageview上方有一个边距,但它不是imageview的一部分。这实际上是因为应用于imageview上方textview的边距。您可以执行以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#00ffff"
android:orientation="vertical"
android:padding="10dp"
>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="Hello Ubuntu (16.06LTS)"
    android:background="#000000"/>

<ImageView
    android:layout_width="371dp"
    android:layout_height="match_parent"
    android:padding="0dp"
    android:background="#ff0000"
    android:layout_margin="0dp"
    />

编辑 - 在上面的代码中,您已将边距应用于视图的所有边。 (android:layout_margin="8dp“) 这给出了包括底部边缘在内的所有4个边的视图边距,它显示为imageview的上边距。

因此,您需要做的是通过更改将边距并排应用于视图     android:layout_margin="8dp"

android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginUpper="8dp"

编辑2 - 确保父(根)容器中有0填充和0边距。

答案 2 :(得分:1)

你可以尝试

android:adjustViewBounds="true"

它对我有用。

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical"
    android:padding="10dp"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_margin="8dp"
        android:text="Hello Ubuntu (16.06LTS)"
        android:background="@color/colorPrimary"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/asd"
        android:layout_margin="0dp"
        android:adjustViewBounds="true"

        />
</LinearLayout>

答案 3 :(得分:1)

这些多余的填充是自动生成的,因为android会尝试获取原始的宽高比。请在下面尝试

scaletype = "fitCenter"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"