2017-02-21 75 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

已經解決或沒有工作? –