2017-02-21 36 views
1

[![enter] image description [1]] [1]我是android到android开发的新品牌,只是用xml来构建布局。我写了下面的代码来打印hello ubuntu并在其中插入图像。但该图像似乎有一个不需要的上下填充或边距(不知道它叫什么),看起来更奇怪。请任何人帮我删除它。这里是我的代码:ImageView中的不需要的填充或边距

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

截图这里(见右侧preiew):https://i.stack.imgur.com/j9NlT.png

+0

检查你的Ubuntu命名图像。它是否有额外的空间? – Piyush

+0

你能为它如何实际上看起来添加图像和如何你想它是 –

+0

PLZ提供屏幕图像 –

回答

0

您应该使用android:scaleType(见documentation)。 这将允许您告诉视图如果图像不具有视图的确切大小如何反应。

+0

我已经尝试过,但没有任何实际发生 – ShivamProgramer

+0

您是否尝试过SEL 'centerCrop'?而且根据你的屏幕截图,它看起来像图像与人交谈。也许你可以尝试为图像视图设置背景颜色以查看图像是否填满了所有imzgr视图? – sonic

+0

是的,我尝试使用centreCrop,但没有工作 – ShivamProgramer

0

imageview上方有一个边距,但它不是imageview的一部分。这实际上是因为将margin应用于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"

EDIT 2侧 - 请确保您有在父(根)容器0填充和0保证金

+0

请详细说明你改变了什么,为什么 – ShivamProgramer

+0

即使 – ShivamProgramer

0

您可以尝试

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> 
+0

已经解决或没有工作? –