简体   繁体   English

ImageView 中不需要的填充或边距

[英]Unwanted paddings or margin in ImageView

I am brand new to android to android development and just playing around with xmls for building layouts.我是 android 到 android 开发的新手,只是玩弄 xmls 来构建布局。 I wrote the following code stuffs for printing a hello ubuntu and inserting an image in it.我编写了以下代码来打印 hello ubuntu 并在其中插入图像。 But the Image seems to have a unwanted upper and bottom padding or margin (not sure what it's called) which looks wierd.但是图像似乎有不需要的上下填充或边距(不确定它叫什么)看起来很奇怪。 Please anyone help me removing it.请任何人帮我删除它。 Here is my code:这是我的代码:

<?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>

Screenshot here(see the preiew on right side):截图在这里(见右侧预览):

1个

those extra padding is autogenerated since the android would try to get the original aspect ratio.这些额外的填充是自动生成的,因为 android 会尝试获得原始的纵横比。 please try below请尝试以下

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

You should use android:scaleType (see documentation ).您应该使用android:scaleType (请参阅文档)。 This will allow you to tell the view how to react if the image does not have the exact size of the view.如果图像没有视图的确切大小,这将允许您告诉视图如何反应。

You can try你可以试试

android:adjustViewBounds="true" , android:adjustViewBounds="true" ,

it works for me.这个对我有用。

   <?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>

There is a margin above the imageview but it is not the part of imageview.图像视图上方有一个边距,但它不是图像视图的一部分。 It is actually because of margin applied to the textview above the imageview.这实际上是因为边距应用于图像视图上方的文本视图。 You can do the following code.您可以执行以下代码。

<?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"
    />

EDIT - In your above code, you have applied Margin to all the sides of the view.编辑 -在上面的代码中,您已将边距应用到视图的所有边。 ( android:layout_margin="8dp ") This gives the margin to the view from all the 4 sides including the bottom one, which appears as the imageview's top margin. ( android:layout_margin="8dp ") 这为视图的所有 4 个边提供边距,包括底部边,它显示为 imageview 的顶部边距。

Hence, what you need to do is to apply margin to the view side by side by changing android:layout_margin="8dp" to因此,您需要做的是通过将android:layout_margin="8dp"更改为并排将边距应用于视图

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

EDIT 2 - Make sure you have 0 padding and 0 margin in the parent (root) container.编辑 2 -确保父(根)容器中有 0 个填充和 0 个边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM