每个人,首先,我在android项目中遇到问题。我将图像设置为具有适当宽度(5.5)的布局背景,当我运行该应用程序时,它显示为白色背景而不是预期的图像。图像通常位于可绘制文件夹中。
图片具有与版式相同的尺寸(5.5)
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:animateLayoutChanges="true"
android:background="@drawable/fundo"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginTop="200dp"
app:srcCompat="@drawable/logo_branco" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:fontFamily="@font/advent_pro_thin"
android:text="O automóvel ideal para si!"
android:textColor="#E76C00"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
答案 0 :(得分:0)
如果要加载整个图像,我将使用在布局的根ViewGroup上将ImageView
设置为两个尺寸都设置为match_parent
的方法。
这将解决图像无法加载的问题,其另外一个好处是可以定义比例类型(例如centerCrop
),该比例类型使您可以以任意宽高比拍摄图片并确保将其填满屏幕在垂直和水平方向上都是如此。
编辑:
建议的解决方案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:animateLayoutChanges="true"
tools:context=".MainActivity">
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/fundo" />
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginTop="200dp"
app:srcCompat="@drawable/logo_branco" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:fontFamily="@font/advent_pro_thin"
android:text="O automóvel ideal para si!"
android:textColor="#E76C00"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>