【问题标题】:Splash Screen image background in LinearLayout not fitLinearLayout 中的启动画面图像背景不适合
【发布时间】:2017-04-03 05:33:45
【问题描述】:

我在初始屏幕活动中使用图像作为 LinearLayout 的背景,但它显示不适合屏幕。我不知道如何使用它的属性来拉伸它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myproject.SplashActivity"
    android:orientation="horizontal"
    android:background="@drawable/my_image_background">

</LinearLayout>

【问题讨论】:

    标签: android android-layout imageview android-xml splash-screen


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.myproject.SplashActivity"
        android:orientation="horizontal"
        android:background="@drawable/my_image_background">
         <ImageView
             android:scaleType="fitXY"
             android:src="@drawable/my_image_background"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>
    </LinearLayout>
    

    使用 ImageView 并使用属性 scaleType 。这将自动适应任何屏幕分辨率。

    ----- 更新-------

    正如Ahamed 所建议的那样,如果你想在布局中添加更多视图,最好使用RelativeLayout(因为它会堆叠你的视图)。考虑下面的RelativeLayout实现。

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/activity_splash"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
             <ImageView
                 android:scaleType="fitXY"
                 android:src="@drawable/my_image_background"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>
              <!-- Add other views depending on your screen specs -->
        </RelativeLayout>
    

    【讨论】:

    • 他需要背景图片。如果他在 Linearlayout 中设置 imageview,他如何放置其余的布局?你能解释一下吗?
    • @Ahamed 我刚刚给出了一个用于启动画面的通用示例。可以使用 RelativeLayout 而不是 LinearLayout 来解决您所说的问题。
    • 是的。你做到了。我很感激。但是,一些新的基础程序员不知道下一步该做什么。因此,请提供有关您的答案的更多信息,然后您将获得更好的声誉。
    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/dighsplash">
    </LinearLayout> 
    

    试试这个方法。

    【讨论】:

      【解决方案3】:

      最好将 ImageView 用于图像,因此我已将您的布局更改如下,它应该可以按您的预期工作。

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/activity_splash"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          tools:context="com.myproject.SplashActivity">
      
          <ImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:scaleType="fitXY"
              android:src="@drawable/my_image_background" />
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        使用这个:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.myproject.SplashActivity"
        android:fitSystemWindows="true"
        android:orientation="horizontal"
        android:scaleType="fitXY"
        android:background="@drawable/my_image_background">
        

        此代码绝对适合您的屏幕。

        【讨论】:

          【解决方案5】:

          正确的方法是这样,所以改用这个,在你的styles.xml中添加这个:

           <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
              <item name="android:windowBackground">@drawable/splash</item>
          </style>
          

          然后在你的 Manifest 文件中添加这个

            <activity
                  android:name=".SplashActivity"
                  android:theme="@style/SplashTheme">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
          
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          

          和@drawable/splash 可能是这样的

          <?xml version="1.0" encoding="utf-8"?>
          <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
              <!-- The background color, preferably the same as your normal theme -->
              <item android:drawable="@drawable/splashscreen"/>
              <!-- Your product logo - 144dp color version of your app icon -->
              <item>
                  <bitmap
                      android:src="@drawable/splash_logo"
                      android:gravity="center"/>
              </item>
          </layer-list> 
          

          【讨论】:

          • 如果您的图像 'splash_logo' 大小 > 2k,您可以将其缩放以适合您的屏幕
          【解决方案6】:

          首先检查图像my_image_background 两侧是否有空格。如果没有,您的代码可以工作...否则您也可以尝试此代码

           <?xml version="1.0" encoding="utf-8"?>
                  <RelativeLayout         xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:tools="http://schemas.android.com/tools"
                      android:id="@+id/activity_splash"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      tools:context="com.myproject.SplashActivity"
                      android:orientation="vertical"
                      android:layout_alignParentRight="true"
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentTop="true"
                      android:layout_alignParentBottom="true"
                      android:background="@drawable/my_image_background">
          
                  </RelativeLayout>
          

          也看看这个::: Showing Splash screen in android apps

          【讨论】:

            【解决方案7】:

            如果 scaleType="fitxy" 不适用于 imageview,请尝试以下操作。

            android:fitSystemWindows="true"

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/activity_splash"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="com.myproject.SplashActivity"
             android:orientation="horizontal"
             android:background="@drawable/my_image_background"
             android:fitSystemWindows="true">
            

            【讨论】:

            • 这里需要什么fillviewport=true。我认为这是滚动视图属性。编辑你的答案。
            猜你喜欢
            • 1970-01-01
            • 2018-04-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-23
            • 2010-10-23
            相关资源
            最近更新 更多