【问题标题】:how to add background image to activity?如何将背景图像添加到活动?
【发布时间】:2011-03-19 10:29:27
【问题描述】:

使用主题或 ImageView ?

【问题讨论】:

    标签: android image background


    【解决方案1】:

    在您的 xml 中使用 android:background 属性。如果要将其应用于整个活动,最简单的方法是将其放在布局的根目录中。所以如果你有一个 RelativeLayout 作为你的 xml 的开始,把它放在这里:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rootRL"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@drawable/background">
    </RelativeLayout>
    

    【讨论】:

    • 不幸的是,这可能会导致一些问题...确保它适用于您的主要活动,但如果您使用相同的 android:background="@drawable/background" 打开一个新活动,如在大多数情况下,当您旋转模拟器和某些设备时,如果您的 bgImg 是高分辨率,您将因“内存不足..”错误而崩溃。 @Sephy,你会如何处理这个问题?
    【解决方案2】:

    您可以通过设置android:background xml 属性来将“背景图片”设置为活动,如下所示:

    (例如,这里为一个活动设置一个线性布局并为布局设置一个背景图像(即间接到一个活动))

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01" 
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:background="@drawable/icon">
     </LinearLayout>
    

    【讨论】:

    • 我注意到这样做对于 LinearLayout 来说效果很好,但对于 RelativeLayouts,我会遇到 OOM 错误。我已经阅读了大量的 decodeBitmap 帖子或 layout.invalidate() 帖子,或 Bitmap.recycle 帖子等...您如何使用 RelativeLayouts 将图像保留为应用程序的背景?我是否应该缓存将出现在多个活动中的任何图像?最佳做法是什么?
    【解决方案3】:

    并且不要忘记在写完这些行之后清理你的项目,你会在你的 xml 文件中得到一个错误,直到你在 eclipse 中清理了你的项目:Project->Clean...

    【讨论】:

      【解决方案4】:

      将图片放在drawable文件夹中。可绘制文件夹位于 res 中。 drawable 有 5 种变体 可绘制的 hdpi 可绘制的 ldpi 可绘制的 mdpi 可绘制的 xhdpi 可绘制-xxhdpi

      【讨论】:

      • 我们必须使用哪个文件夹? @Jesvin
      【解决方案5】:

      现在我们必须使用match_parent

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:orientation="vertical" 
          android:background="@drawable/background">
      </RelativeLayout>
      

      【讨论】:

        【解决方案6】:

        我们可以使用 ImageView 轻松地将背景图像放置在 PercentFrameLayout 中。我们必须设置 scaleType 属性 value="fitXY" 并且在前台我们还可以显示其他视图,例如 textview 或 button。

         <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                >
                <ImageView
                    android:src="@drawable/logo"
                    android:id="@+id/im1"
                    android:scaleType="fitXY"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"/>
        <EditText android:layout_gravity="center_horizontal"
                android:hint="Enter Username"
                android:id="@+id/et1"
                android:layout_height="wrap_content"
                app:layout_widthPercent="50%"
                app:layout_marginTopPercent="30%"
                />
        <Button
            android:layout_gravity="center_horizontal"
            android:text="Login"
            android:id="@+id/b1"
            android:layout_height="wrap_content"
            app:layout_widthPercent="50%"
            app:layout_marginTopPercent="40%"/>
        </android.support.percent.PercentFrameLayout>
        

        【讨论】:

          【解决方案7】:

          ez方式:复制你的图片到这个位置:C:\Users\Your user name\AndroidStudioProjects\yourProjectName\app\src\main\res\drawable

          并在您的 xml 文件中编写此代码:

           <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:background="@drawable/background">
          
          </RelativeLayout>
          

          在第 7 行写下您的文件名,例如 background

          还有塔玛姆……

          【讨论】:

            猜你喜欢
            • 2015-12-26
            • 2016-02-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-06
            • 2012-04-09
            相关资源
            最近更新 更多