【问题标题】:Android, can't see my app background imageAndroid,看不到我的应用背景图片
【发布时间】:2015-08-31 15:18:40
【问题描述】:

我的应用程序的背景图像有问题,我已将其设置为 android:background="@drawable/image_name" 并且在 android studio 的预览中工作正常,但是,当我运行应用程序,我看不到图像,只有白色背景,有人可以帮助我吗?

这是我的 xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash"
tools:context="com.example.ivan.posteggiaTI.LoginActivity">

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_above="@+id/editTextLoginEmail"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="48dp" />

<EditText
    android:id="@+id/editTextLoginEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/login_edit_text"
    android:hint="Email"
    android:inputType="textEmailAddress"
    android:paddingBottom="0dp"
    android:paddingLeft="15dp"
    android:paddingRight="10dp"
    android:paddingTop="0dp"
    android:textColor="#000000"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="22dp"
    android:layout_above="@+id/editTextLoginPassword"
    android:layout_alignParentStart="true" />

<EditText
    android:id="@+id/editTextLoginPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/login_edit_text"
    android:hint="Password"
    android:inputType="textPassword"
    android:paddingBottom="0dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="0dp"
    android:textColor="#000000"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_above="@+id/textViewRegister"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="34dp" />

<TextView
    android:id="@+id/textViewRegister"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button_blue_login"
    android:gravity="center_vertical|center_horizontal"
    android:text="Registrati"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff"
    android:layout_alignTop="@+id/textViewLogin"
    android:layout_toStartOf="@+id/textViewLogin" />

<TextView
    android:id="@+id/textViewLogin"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/button_blue_login"
    android:gravity="center_vertical|center_horizontal"
    android:text="Login"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff"
    android:layout_marginBottom="27dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" /></RelativeLayout>

【问题讨论】:

  • 试过这段代码,在我的设备上运行良好,棒棒糖
  • 尝试像这里stackoverflow.com/questions/12523005/…这样以编程方式从代码更改背景
  • 哪个代码?你忘了...
  • @DavidBalas 我以这种方式尝试过RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutLogin); relativeLayout.setBackground(getResources().getDrawable(R.drawable.splash)); 但不起作用
  • 您能告诉我启动图像的大小以及您将它保存在哪个可绘制文件夹中吗?

标签: android background-image drawable xml-drawable


【解决方案1】:

有时 android studio 在运行时不会安装新的 apk。它将先前的活动置于前面。

按照步骤操作。

  1. 确保您在可绘制文件夹中有图片。
  2. 重建应用程序。构建->重建应用程序
  3. 从模拟器或手机(测试设备)卸载应用程序。
  4. 运行应用程序。

当您将图像放入可绘制文件夹时。适当地选择您想要哪张图片的屏幕密度。

因为有drawable-ldpi、drawable-mdpi、drawable-hdpi等...和一个普通的drawable文件夹。如果 android studio 在这些限定符可绘制文件夹中找不到图像,那么它将在普通的可绘制文件夹中查找。

【讨论】:

    【解决方案2】:

    使用ImageView 作为RelativeLayout 中的第一个元素。然后,将ImageViewdrawable 设置为src,并将scaleType 设置为fitXY,以便正确显示背景。

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多