如何缩放大小并将图像放置在 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>
最好的方法是什么,因为设置前缀大小是因为我不允许调整到更大的屏幕(主要是平板电脑)。
尝试将您的 ImageView 添加到 linearLayout 或相对布局的父级中。
然后添加带 android:scaleType="center" 的 ImageView,如果使用相对或 android:gravity="center" 线性布局,也不要忘记添加 centerInParent = true。
由于您正在为父面板使用约束布局,请尝试以下操作:
<?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 的中心,以便在 center.As 下面设置子级
<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>
我的确切目的是稍微减小图像的尺寸(高度和宽度)(因为 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>
最好的方法是什么,因为设置前缀大小是因为我不允许调整到更大的屏幕(主要是平板电脑)。
尝试将您的 ImageView 添加到 linearLayout 或相对布局的父级中。
然后添加带 android:scaleType="center" 的 ImageView,如果使用相对或 android:gravity="center" 线性布局,也不要忘记添加 centerInParent = true。
由于您正在为父面板使用约束布局,请尝试以下操作:
<?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 的中心,以便在 center.As 下面设置子级
<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>