2017-07-24 123 views
0

我有一个活动,我设置了imageview元素,以匹配家长与我的背景图像之一与维度:1440x2560。我需要使这个形象充分。但每当我把使用scaleType = “fitXY” 的形象我得到这个: enter image description hereAndroid背景图像 - imageview

XML布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.backgroundApp.SecondActivity"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:srcCompat="@drawable/background" 
     android:scaleType="fitXY"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <TextView 
      android:id="@+id/textView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="TextView"/> 

    </LinearLayout> 

</android.support.v4.widget.DrawerLayout> 

活动:

package com.backgroundApp; 

import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class SecondActivity extends AppCompatActivity { 

    ActionBar actionBar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 

     actionBar = getSupportActionBar(); 
     actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004d40"))); 
    } 
} 
+1

你尝试'scaletype = centerCrop'? –

+0

你已经有答案了吗? – UmarZaii

+0

@AjilO。它有点大,但不是全屏... – SteveYo

回答

2

使用adjustViewBounds =真 property in ImageView tag like

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:srcCompat="@drawable/background" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY"/> 
+0

我得到这个错误:错误:(9)没有找到包'com.backgroundApp'中的属性'adjustViewBounds'的资源标识符 – SteveYo

+0

尝试使用android:adjustViewBounds。它会工作 –

+2

请检查我改变了我的答案 – Anil

1

使用setAdjustViewBounds

setAdjustViewBounds(boolean adjustViewBounds)

如果你想ImageView的调整它的界限,以保持其绘制的长宽比设置为true。

<ImageView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY"/> 

或尝试设置编程

imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 
imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
imageView.setAdjustViewBounds(true); 
+0

我收到此错误:错误:(9)找不到包含'com.backgroundApp'属性'adjustViewBounds'的资源标识符 – SteveYo

+0

声明名称空间xmlns:app =“http://schemas.android.com/apk/ res-auto“ –

+2

请尝试使用android:adjustViewBounds。它会工作 –

0

变化

app:srcCompat="@drawable/background" 

android:src="@drawable/xxxxx"