android - LinearLayout 中的启动画面图像背景不适合

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

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

<?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>

最佳答案

<?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 。这将自动适应任何屏幕分辨率。

----- Update -------

根据 Ahamed 的建议除此之外,如果您想在布局中添加更多 View ,最好使用 RelativeLayout(因为它会堆叠您的 View )。考虑以下 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>

关于android - LinearLayout 中的启动画面图像背景不适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43177135/

相关文章:

ios - UICollectionView 无法更改单元格的 imageView 大小

android - EditText 斜体样式剪切第一个字母

android - 如何在底部导航栏上绘制顶部边框

安卓工作室 : changing imageview weekly

ios - tableview cell.imageview 没有动画

android - 传递上下文时出错

javascript - 通过移动在 Chrome 中模拟地理位置

java - TeeChart 3d 饼图厚度

android - 以编程方式设置代理用户名和密码Android

android - 枚举结果时出错,请检查原始异常以获取详细信息