【问题标题】:Android - Constraint Layout - Alignment of Text and IconAndroid - 约束布局 - 文本和图标的对齐方式
【发布时间】:2020-04-05 19:22:11
【问题描述】:


首先,我是 Android 开发的新手。
我的问题是关于 Android Studio/Development 中不同视图的对齐方式。 特别是图标和文本的正确高度对齐。

Icon-Text-Alignment

如您所见,我尝试将文本与图标对齐。然而,结果在模拟器中看起来略有不同。

这是我的 .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"
    tools:context=".DashboardActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginBottom="352dp"
        android:fontFamily="@font/poppins_medium"
        android:text="Dashboard"
        android:textAlignment="viewStart"
        android:textColor="@color/colorText"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageView10"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.04" />

    <ImageView
        android:id="@+id/imageView10"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="32dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="@+id/textView3"
        app:srcCompat="@drawable/ic_settings_black_24dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的问题是:

1. 如何修复这两个视图的对齐方式?
2. 为什么模拟器中的结果不一样?
3. 是否有正确对齐视图的最佳实践?

【问题讨论】:

    标签: android android-studio android-layout android-view


    【解决方案1】:
     <?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"
        tools:context=".DashboardActivity">
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginBottom="352dp"
            android:fontFamily="@font/poppins_medium"
            android:text="Dashboard"
            android:textAlignment="viewStart"
            android:textColor="@color/colorText"
            android:textSize="30sp"
    
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/imageView10" />
    
        <ImageView
            android:id="@id/imageView10"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginEnd="32dp"
            app:srcCompat="@drawable/ic_settings_black_24dp"
    
            app:layout_constraintTop_toTopOf="@id/textView3"
            app:layout_constraintBottom_toBottomOf="@id/textView3"
            app:layout_constraintEnd_toEndOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 出现同样的问题。在模拟器中它看起来又有点不对劲了。
    【解决方案2】:

    您只需使用自定义工具栏并向其添加菜单即可。

    【讨论】:

      【解决方案3】:

      尝试使用Custom toolbar

      <LinearLayout 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:orientation="vertical"
          tools:context=".terminalShortActivity">
      
      
          <androidx.appcompat.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="#4EACE2"
              app:popupTheme="@style/ThemeOverlay.AppCompat">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:gravity="center_vertical"
                  android:orientation="horizontal">
      
                  <TextView
                      style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Terminal Shortcuts"
                      android:textColor="#FFFFFF" />
              </LinearLayout>
      </androidx.appcompat.widget.Toolbar>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        所以我进一步深入研究了对齐问题,并提出了一些“解决方案”。一般来说,为了回答我的问题,“自定义工具栏”不是对齐视图的正确解决方案。

        问题在于字体的填充。 Android 采用整个视图来对齐视图,而不是仅假设“文本”。因此对齐看起来不正确。

        Different paddings/fonts

        1. Poppinsmedium:没有填充
        2. Poppinsmedium:有填充;正常状态
        3. Android-Font:带内边距;正常状态

        可以使用以下代码消除底部填充:

        android:includeFontPadding="false"
        

        或查看此线程以更深入地了解issues of font-padding

        为了对齐视图,我现在使用 Chain-Constraints 并且没有填充。它看起来是对齐视图的最佳方式,尤其是文本视图。但是,我不得不说,尽管删除了底部填充,但还是需要稍微调整一下垂直偏差的对齐方式。

        供参考:

        constraintlayout.com

        Android-Doc: constrain-chain

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-28
          • 1970-01-01
          相关资源
          最近更新 更多