【问题标题】:How do I add a background image to my app?如何为我的应用添加背景图片?
【发布时间】:2017-08-05 05:41:11
【问题描述】:

如何添加图片并将其设置为我的应用背景

我知道您将所需的图像粘贴到可绘制文件夹中。这是代码本身的问题,还是我应该创建一个 RelativeLayout?还是只是在提供的代码中进行了一行更改?

这是我的代码,没有更改任何行。

<?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="anaheim.startercalculator.MainActivity">

<EditText
    android:id="@+id/firstNumEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="48dp"
    android:ems="10"
    android:hint="Enter a value"
    android:inputType="number"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<EditText
    android:id="@+id/secondNumEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="Enter a value"
    android:inputType="number"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/firstNumEditText"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<Button
    android:id="@+id/addBtn"
    android:layout_width="95dp"
    android:layout_height="45dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="58dp"
    android:text="ADD"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/secondNumEditText"
    tools:layout_editor_absoluteX="143dp" />

<Button
    android:id="@+id/multBtn"
    android:layout_width="95dp"
    android:layout_height="45dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="MULTIPLY"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/addBtn"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    tools:layout_editor_absoluteY="263dp"
    tools:layout_editor_absoluteX="143dp"
    app:layout_constraintHorizontal_bias="0.501" />

<TextView
    android:id="@+id/resultTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="48sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="139dp"
    app:layout_constraintTop_toBottomOf="@+id/multBtn"
    app:layout_constraintHorizontal_bias="0.501"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<Button
    android:id="@+id/subBtn"
    android:layout_width="98dp"
    android:layout_height="45dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:text="Subtract"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintVertical_bias="0.027"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/multBtn"
    tools:layout_editor_absoluteY="323dp"
    tools:layout_editor_absoluteX="143dp"
    app:layout_constraintHorizontal_bias="0.503" />

<Button
    android:id="@+id/divBtn"
    android:layout_width="95dp"
    android:layout_height="45dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="DIVIDE"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/subBtn"
    tools:layout_editor_absoluteY="377dp" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 非常简单,添加到您的 ConstratintLayout:android:background="@drawable/your_image"

标签: android android-layout


【解决方案1】:

如果你想为应用设置背景,那么你可以在你的主题中使用windowBackground

<item name="android:windowBackground">@drawable/sample_bg</item>

您可以为任何视图或视图组提供背景,在 xml 中您可以像这样给出 android:background="[your background image or color]"。

使用颜色 - android:background="@color/colorPrimary" 使用可绘制 - android:background="@drawable/sample_bg"

如果您想为完整布局提供背景,那么您可以为您的根布局提供背景(在您的情况下,ConstraintLayout 布局是根)

注意:

对于按钮,你可以给color state list,对于ImageView你 也可以给背景和源图片。

【讨论】:

    【解决方案2】:

    如果您想为完整布局提供背景,请使用以下代码:

    <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"
        **android:background="@drawable/ic_add"**>
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      把这段代码放到你的xml中;

        <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="anaheim.startercalculator.MainActivity">
      
      
      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      xmlns:ads="http://schemas.android.com/apk/res-auto">
      
      
      <ImageView
          android:id="@+id/logo"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:src="@drawable/login_0018_background"
          android:scaleType="fitXY"/>
      
          <Write your code xml here>
      
      
      </RelativeLayout>
      

      【讨论】:

      • 请避免添加不必要的嵌套布局和不必要的视图。嵌套布局不利于性能。
      【解决方案4】:

      将 RelativeLayout 添加到您的根目录中,并将所有 EditText 放入其中。 然后使用相对布局的背景属性将您的可绘制对象设置为相对布局的背景。

      【讨论】:

      • 请避免添加不必要的嵌套布局。嵌套布局不利于性能。
      【解决方案5】:

      android:background="@drawable/back"

      ConstraintLayout 中添加以上行并记住back 是我的背景图片名称

      【讨论】:

        【解决方案6】:

        android:src="@drawable/your_image" android:scaleType = "centerCrop"

        【讨论】:

          【解决方案7】:

          您告诉 ImageView 将其顶部限制在按钮的顶部

          <android.support.constraint.ConstraintLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="mreram.testapp.MainActivity">
          <ImageView
              android:scaleType="centerCrop"
              android:src="..."
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              android:layout_width="0dp"
              android:layout_height="0dp" />
          
          <Write your code xml here>
          

          【讨论】:

          • 您确实不需要在现有布局中添加额外的 ImageView。
          • @ModularSynth 你不能用背景属性缩放图像!
          • 背景图片会自动缩放以适合其容器。
          • @ModularSynth 将图像设置为背景以填充视图...它拉伸了图像,这样看起来不太好
          • 背景图片就是这样工作的。您也可以使用其他类型的资源,例如:颜色、渐变、9 个色块……
          【解决方案8】:

          你可以通过两种方式做到这一点。

          第一个是将图像设置为约束布局的背景使用

          android:background="@drawable/bg_image"
          

          或者添加一个imageview作为constraintlayout的第一个元素

          <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">
          
          <ImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:src="@drawable/bg_image"
              android:scaleType="fitXY"/>
          </android.support.constraint.ConstraintLayout>
          

          【讨论】:

          • 第二种方式很差。您真的不需要在现有布局中添加额外的 ImageView。
          【解决方案9】:

          打开res -&gt; values -&gt; styles.xml 并在您的&lt;style&gt; 中添加此行,替换为您的图像路径&lt;item name="android:windowBackground"&gt;@drawable/background&lt;/item&gt;。示例:

          <resources>
          
              <!-- Base application theme. -->
              <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
                  <!-- Customize your theme here. -->
                  <item name="colorPrimary">@color/colorPrimary</item>
                  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                  <item name="colorAccent">@color/colorAccent</item>
                  <item name="android:windowBackground">@drawable/background</item>
              </style>
          
          </resources>
          

          还有一个&lt;item name ="android:colorBackground"&gt;@color/black&lt;/item&gt;,它不仅会影响您的主窗口背景,还会影响您应用中的所有组件。

          Read more here(doc in theme).

          如果你想添加android版本特定的样式,read here或使用上面的链接。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-02-13
            • 1970-01-01
            • 1970-01-01
            • 2020-07-09
            • 1970-01-01
            • 2014-07-16
            • 2023-02-08
            • 2018-04-08
            相关资源
            最近更新 更多