繁体   English   中英

如何缩放大小并将图像放置在 Android 应用程序启动画面的中心

[英]How to scale size and place image in center of Splash Screen of Android App

我的确切目的是稍微减小图像的大小(高度和宽度)(因为 wrap_content 大小太大)并将其放置在启动画面的中心。 但是无论我做什么,在我使用 wrap_content 或 match_parent 之前它都不会进入中心。 这是我正在使用的代码。

 <?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="com.yoalfaaz.yoalfaaz.SplashScreen">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        tools:layout_editor_absoluteY="25dp"
        tools:layout_editor_absoluteX="25dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ss" />
</android.support.constraint.ConstraintLayout>

什么是最好的方法,因为设置前缀大小我不会让调整到更大的屏幕(主要是平板电脑)。

由于您为父面板使用了约束布局,请尝试以下操作:

<?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="com.yoalfaaz.yoalfaaz.SplashScreen">
    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:src="@drawable/ss" />
</android.support.constraint.ConstraintLayout>

如果您使用 LinearLayout 作为父级,则需要将重力设置为父级 LinearLayout 的中心以将子级设置为中心。如下

     <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <ImageView
                        android:layout_width="300dp"
                        android:layout_height="300dp"
                        android:layout_gravity="center"
                        tools:layout_editor_absoluteY="25dp"
                        tools:layout_editor_absoluteX="25dp"
                        android:src="@drawable/ss"
                        />
                </LinearLayout>
 //  if you are using RelativeLayout then :
      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"
            tools:layout_editor_absoluteY="25dp"
            tools:layout_editor_absoluteX="25dp"
            android:src="@drawable/ss"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>

尝试将您的 ImageView 添加到 linearLayout 或相对布局的父级中。

然后添加带有 android:scaleType="center" 的 ImageView,如果使用相对或 android:gravity="center" 为线性布局,也不要忘记添加 centerInParent = true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM