【问题标题】:Change background image of drawable resource更改可绘制资源的背景图像
【发布时间】:2023-03-19 07:56:01
【问题描述】:

基本上我创建了一个 background.xml 来添加背景图像以保持纵横比。然后我将ImageView的背景设置为@drawable/background

如何更改 Activity 的背景图片?

在activity_main.xml中:

<ImageView
            android:scaleType="centerCrop"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:id="@+id/imageView1"
            android:layout_alignTop="@+id/scrollView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="@drawable/background"
            android:src="@drawable/gradient"/>

在 background.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="fill"
    android:src="@drawable/image" />

@drawable/image 是实际的 .jpg 文件

【问题讨论】:

  • 为什么你有一个 background.xml?只要在ImageView中设置android:background="@drawable/image就可以了
  • spearhend.blogspot.in/2012/04/… 看看这是否可行。
  • 为什么是 android:background??我敢打赌你的意思是 android:src???
  • @EllieZou 我这样做是因为直接使用@drawable/image 会破坏图像的纵横比。你知道保持背景图片纵横比的方法吗?
  • @pskink 我这样做是因为我想要图像上的渐变。所以 src 是可绘制的渐变

标签: android xml android-drawable xml-drawable


【解决方案1】:

您可以像这样以编程方式设置图像视图

ImageView image;
//initialization
image.setImageResource(int);

// int 是像 R.drawable.something 这样的资源

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多