布局中的XML背景图像

时间:2017-12-14 18:04:42

标签: android xml android-layout background

这很荒谬,没有错误,我已经在其他一些项目上做了同样的工作并且工作正常但它仍然没有显示任何内容...... 有人知道为什么我的背景图像在编辑器中很好(在Android Studio编辑器中显示背景图像),但是当我在我的设备上运行应用程序时,它只显示我的2个按钮的唯一白色背景......

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.example.ale.mediaspectrumplayer.MainActivity"
android:background="@drawable/background">

    <Button
        android:id="@+id/ID_MediaPlayer_Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="48dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="24dp"
        android:text="@string/main_MediaPlayer"
        app:layout_constraintBottom_toTopOf="@+id/ID_SpectrumView_Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/ID_SpectrumView_Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="48dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="16dp"
        android:text="@string/main_SpectrumView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.25"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

无论代码是否有效,我都不知道为什么(layout.setBackgroundResource(R.drawable.background),以及图像视图和缩放fitXY ......这都是荒谬的

3 个答案:

答案 0 :(得分:0)

您是否尝试过使用android:scaleType属性?这可以改变背景图像的位置以覆盖屏幕,有时如果图像太小则不会显示。将scaleType设置为centercenterCrop可能会有所帮助。

以下是文档:https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

答案 1 :(得分:0)

尝试将父布局更改为RelativeLayout或任何其他布局。

答案 2 :(得分:0)

有时,如果您用来设置为背景的图像具有高分辨率或很重,系统只是避免加载它来保存内存。如果您希望在整个内容的背景中使用该图像,则必须设置其高度和宽度与父布局的高度和宽度匹配的图像视图。然后使用图像加载库,例如:Picasso,Glide等,在该ImageView中高效加载高分辨率图像,而不会给系统带来负担