我的确切目的是稍微减小图像的大小(高度和宽度)(因为wrap_content大小太大)并将其放在Splash Screen的中心。但无论我做什么,它都不会进入中心,直到我使用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>
最好的方法是什么,因为设置前缀大小,因为我不会让它调整到更大的屏幕(大多数平板电脑)。
答案 0 :(得分:1)
由于您正在为父面板使用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>
答案 1 :(得分:1)
如果您使用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>
答案 2 :(得分:0)
尝试在linearLayout或相对布局的父级内添加ImageView。
然后添加带有android:scaleType =“center”的ImageView,如果使用relative或android:gravity =“center”for linearLayout,也不要忘记添加centerInParent = true。