Android 碎片中的布局未居中

Android 碎片中的布局未居中,android,xml,android-fragments,kotlin,Android,Xml,Android Fragments,Kotlin,我正在为Android构建第一个合适的应用程序。我希望使用导航抽屉活动在屏幕和选项之间切换。到现在为止,一直都还不错。一切都很好。但是我在内容中加载的布局没有正确对齐。看图 我已尝试更改布局宽度和布局高度,以填充、匹配和包装所有组合中的父内容。我也尝试过添加重心和垂直重心,但到目前为止我失败了 内容布局: 但事实上,这就是它看起来的样子: 在所有编辑文本中,最好使用drawablestart而不是imageview 请查找下面的xml,可能会有所帮助 <?xml vers

我正在为Android构建第一个合适的应用程序。我希望使用导航抽屉活动在屏幕和选项之间切换。到现在为止,一直都还不错。一切都很好。但是我在内容中加载的布局没有正确对齐。看图

我已尝试更改布局宽度和布局高度,以填充、匹配和包装所有组合中的父内容。我也尝试过添加重心和垂直重心,但到目前为止我失败了

内容布局:


但事实上,这就是它看起来的样子:

在所有编辑文本中,最好使用drawablestart而不是imageview 请查找下面的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">

      <EditText
         android:id="@+id/editText11"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="64dp"
         android:layout_marginEnd="8dp"
         android:drawableStart="@mipmap/ic_launcher"
         android:ems="10"
         android:hint="Naziv klijenta"
         android:inputType="textPersonName"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.518"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />

      <EditText
         android:id="@+id/editText9"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="24dp"
         android:drawableStart="@mipmap/ic_launcher"

         android:ems="10"
         android:hint="Kontakt broj"
         android:inputType="textPersonName"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/editText11" />
    <EditText
    android:id="@+id/editText10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:drawableStart="@mipmap/ic_launcher"
    android:ems="10"
    android:hint="Adresa"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText9" />


<EditText
    android:id="@+id/editText12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:drawableStart="@mipmap/ic_launcher"
    android:ems="10"
    android:hint="E-Mail"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText10" />


<Spinner
    android:id="@+id/spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:entries="@mipmap/ic_launcher"
    app:layout_constraintEnd_toEndOf="@+id/editText12"
    app:layout_constraintStart_toStartOf="@+id/editText12"
    app:layout_constraintTop_toBottomOf="@+id/editText12" />

 <Spinner
    android:id="@+id/spinner2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:entries="@mipmap/ic_launcher"
    app:layout_constraintEnd_toEndOf="@+id/spinner"
    app:layout_constraintStart_toStartOf="@+id/spinner"
    app:layout_constraintTop_toBottomOf="@+id/spinner" />

   <Button
      android:id="@+id/button"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="128dp"
      android:layout_marginTop="8dp"
      android:layout_marginEnd="128dp"
      android:layout_marginBottom="32dp"
      android:text="xxxx"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/spinner2"
      app:layout_constraintVertical_bias="1.0" />
   </androidx.constraintlayout.widget.ConstraintLayout>

如果您想在左侧使用imageview的特定大小,请使用下面的代码

    <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
       >
      <item
      android:drawable="@drawable/icon"
      android:width="@dimen/icon_size"
      android:height="@dimen/icon_size"
      />

     </layer-list >

您的
内容布局中的
相对版面中的问题

您需要在
内容布局中将
RelativeLayout
宽度更改为
android:layout\u width=“match\u parent”

试试这个

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main_menu"
        tools:context=".MainMenu">


        <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

问题不在于片段布局实现,我只是在一个活动中进行了尝试,它工作正常

我想说的是,要将内容布局更改为FrameLayout(通常用于保存片段),可以在此处找到更多信息:

应使用框架布局而不是相对布局

<FrameLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

只需将id保留为relativeLayout,但可以随意更改它


此外,如果您有这样的问题,您可以始终使用布局检查器,它位于“工具”菜单中,请尝试一下。或者,您可以只使用背景色来检查布局大小,就像在布局检查器之前的《时光倒流》一样。

使用FrameLayout而不是相对布局,最后从父约束布局中删除线条

tools:layout_editor_absoluteY="81dp"

您可以使用属性
app:layout\u constraintvertic\u bias
,使其对父对象的偏移相等。这也可以在布局编辑器中通过将布局的所有4个面约束到父面来实现

试着像下面这样使用

<?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.LinearLayoutCompat
        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_gravity="center_vertical"
        android:gravity="center_vertical"
        android:layout_height="match_parent">

        <ImageView
                android:layout_width="34dp"
                android:layout_height="34dp"
                app:srcCompat="@drawable/fragment_client_add_address"
                android:id="@+id/imageView10"
                android:layout_marginEnd="12dp"
                android:layout_marginTop="8dp"/>

    </androidx.appcompat.widget.LinearLayoutCompat>

和主要内容

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</androidx.appcompat.widget.LinearLayoutCompat>


而不是约束使用Linearlayout@JimaleAbdi你的意思是什么?Sry我还是android编程新手给我时间解决你的问题内容布局指的是什么?它是活动的布局还是导航抽屉还是什么?这是什么内容?它是一个片段吗?不要使用LinearLayOut。我的问题是,我想在relativelayout中显示的内容没有像“预期结果”图片中那样居中于左右上下。在这里,您可以看到我添加了约束,以便所有editText字段都居中,但实际上它们被推到了左侧,并且我的按钮完全断开了:/i使用xml运行代码,并为我检查editText是否居中,只有imageview向左移动。当我纠正可绘制的左侧时,所有编辑文本都居中对齐。请让我知道这是否对您有所帮助。对于延迟的响应,很抱歉,但您的解决方案不起作用:/我真不敢相信它会这么简单。。。非常感谢您的解决方案!在发布之前尝试过,没有什么不同。您使用的ConstraintLayout库的版本是什么?我已经完全尝试了您的xml,它可以与“androidx.constraintlayout:constraintlayout:1.1.3”和android 10 emulator配合使用。我正在附加一张sameSeems的图片,我也在使用“androidx.constraintlayout:constraintlayout:1.1.3”,但在我的android 7手机上运行和测试该应用程序我没有更新这篇文章中的代码,但我已经尝试过了,这对我没有帮助。