Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 元素将无法在Constraintlayout Android Studio中正确居中_Java_Android_Android Constraintlayout - Fatal编程技术网

Java 元素将无法在Constraintlayout Android Studio中正确居中

Java 元素将无法在Constraintlayout Android Studio中正确居中,java,android,android-constraintlayout,Java,Android,Android Constraintlayout,我有这样一个启动屏幕: 但如果我打开我的应用程序,它将显示如下: 这是我的XML布局: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res

我有这样一个启动屏幕:

但如果我打开我的应用程序,它将显示如下:

这是我的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@android:color/holo_red_light"
tools:context=".SplashScreen">

<ImageView
    android:id="@+id/splash_icon"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="160dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@mipmap/ic_concas_icon" />

<TextView
    android:id="@+id/splash_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="GO Tools"
    android:textColor="@android:color/white"
    android:textSize="48sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="@+id/splash_icon"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/splash_icon"
    app:layout_constraintTop_toBottomOf="@+id/splash_icon" />

<ProgressBar
    android:id="@+id/splash_progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="136dp"
    app:layout_constraintEnd_toEndOf="@+id/splash_icon"
    app:layout_constraintHorizontal_bias="0.506"
    app:layout_constraintStart_toStartOf="@+id/splash_icon"
    app:layout_constraintTop_toBottomOf="@+id/splash_icon" />

<TextView
    android:id="@+id/splash_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="204dp"
    android:text="STATUS"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="@+id/splash_icon"
    app:layout_constraintHorizontal_bias="0.508"
    app:layout_constraintStart_toStartOf="@+id/splash_icon"
    app:layout_constraintTop_toBottomOf="@+id/splash_icon" />

我需要像Android Studio preview一样正确放置元素,但如果我启动我的应用程序,仍然无法工作。有人知道怎么做吗?非常感谢你


编辑:图像将不会加载,但已正确设置。

看起来您的布局正常,但如果您希望图像更靠近屏幕顶部,则需要更改图像,现在您告诉图像要居中于屏幕中部,以便所有其他视图的位置都低于图像。 一个好的解决方案是添加指南,并将图像限制在指南中,而不是屏幕顶部(这样,图像将被放置得更高一点,从而改变布局外观)

例如:

   <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/splash_icon"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="8dp"
        android:background="@color/buttonColor"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintHorizontal_bias="0.488"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <TextView
        android:id="@+id/splash_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:layout_marginBottom="8dp"
        android:background="@color/buttonColor"
        android:text="GO Tools"
        android:textColor="@android:color/white"
        android:textSize="48sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/splash_progress"
        app:layout_constraintEnd_toEndOf="@+id/splash_icon"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/splash_icon"
        app:layout_constraintTop_toBottomOf="@+id/splash_icon" />

    <ProgressBar
        android:id="@+id/splash_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="136dp"
        android:layout_marginBottom="8dp"
        android:background="@color/buttonColor"
        app:layout_constraintBottom_toTopOf="@+id/splash_status"
        app:layout_constraintEnd_toEndOf="@+id/splash_icon"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintStart_toStartOf="@+id/splash_icon"
        app:layout_constraintTop_toBottomOf="@+id/splash_icon" />

    <TextView
        android:id="@+id/splash_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="204dp"
        android:layout_marginBottom="8dp"
        android:background="@color/buttonColor"
        android:text="STATUS"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/splash_icon"
        app:layout_constraintHorizontal_bias="0.508"
        app:layout_constraintStart_toStartOf="@+id/splash_icon"
        app:layout_constraintTop_toBottomOf="@+id/splash_icon" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3"/>
</androidx.constraintlayout.widget.ConstraintLayout>


编辑:这可能适用于您的手机,但我刚刚注意到您使用的是固定大小的视图-尽量避免这种情况,因为不同的手机有不同的手机大小

您的布局过于依赖于屏幕大小,将您的视图彼此限制在一起,并依赖于不同的边距和垂直方向使其在设计器中工作的偏差。因此,当您移动到另一个屏幕尺寸时,布局不会按您的预期方式工作


查看
ConstraintLayout
,了解如何对小部件进行分组,使其在所有屏幕大小中居中。我认为你需要一个
CHAIN\u-PACKED
CHAIN。

是否可以用符号百分比%指定尺寸?什么尺寸?元素尺寸,以及是否可能在显示器中有%的位置。实现这一点的方法有很多-一种方法是使用屏障/指南,并将它们放在屏幕的百分比上(例如10%的屏幕,告诉你的观点要遵守指导原则)。这确实值得一读,因为有很多例子