【问题标题】:XML background image in layout布局中的 XML 背景图像
【发布时间】:2017-12-14 18:04:41
【问题描述】:

这太荒谬了,没有错误,我已经在其他一些项目上做过同样的事情并且工作正常,但它仍然什么也没显示...... 有人知道为什么我的背景图像在编辑器中很好(在 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 都不起作用......这太荒谬了

【问题讨论】:

    标签: android xml android-layout background


    【解决方案1】:

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

    【讨论】:

    • 我刚刚调整了图像的大小,现在它可以工作了...这很奇怪,因为在我的另一个项目中我使用了与这个大小相同的图像...嗯,无论如何都可以,所以谢谢你:)
    • 实际上并不奇怪,您正在加载图像作为 ConstrainLayout 的背景,它在处理图像方面不如 ImageView 好。如果您尝试在 imageview 中加载原始图像,则获得预期结果的机会会更高。
    • imageview 也没有工作......但我说这很奇怪,因为在我的另一个项目中我加载了一个类似的图像(我的意思是,类似的大小)并且它工作......这就是为什么我说“奇怪”
    【解决方案2】:

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

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

    【讨论】:

    • 他们都不做我想做的事...scaleType: center 只是将图像居中(在 imageview 中)并且它不能作为背景,centerCrop 不会将图像固定为背景,它只是大于屏幕尺寸
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      相关资源
      最近更新 更多