【问题标题】:How to align 2 image view in a row?如何连续对齐 2 个图像视图?
【发布时间】:2021-12-26 00:17:45
【问题描述】:

这是我的 xml 代码,我想在一行中设置两个图像

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:padding="10dp"
    android:background="#ffffff"
    tools:context=".SplashActivity">

    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="left"
        android:src="@drawable/ic_2"
        android:text="Hello World!"
        android:tint="#ff0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_editor_absoluteX="-52dp" />

    <ImageView
        android:layout_width="340dp"
        android:layout_height="340dp"
        android:layout_gravity="right"
        android:src="@drawable/ic_karo"
        android:text="Hello World!"
        android:tint="#0000b3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_editor_absoluteX="123dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

在 Android Studio Preview 中,它看起来很完美。但是当我在设备中安装 APK 时,它不起作用

Android Studio 预览版:

这是我的设备预览:

【问题讨论】:

    标签: android xml android-studio apk


    【解决方案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"
        android:padding="10dp"
        android:background="#ffffff"
        tools:context=".SplashActivity">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="340dp"
                android:layout_height="340dp"
                android:layout_gravity="left"
                android:src="@drawable/ic_2"
                android:text="Hello World!"
                android:tint="#ff0000"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.498"
                tools:layout_editor_absoluteX="-52dp" />
    
            <ImageView
                android:layout_width="340dp"
                android:layout_height="340dp"
                android:layout_gravity="right"
                android:src="@drawable/ic_karo"
                android:text="Hello World!"
                android:tint="#0000b3"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.498"
                tools:layout_editor_absoluteX="123dp" />
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 如果我使用你的代码,那么左边的第一张图片和右边的第二张图片。但我想在中心
    • 你应该添加 layout_gravity 作为 ImageView 的中心。在您当前的代码中,layout_gravity 分别为“左”和“右”。
    【解决方案2】:

    正如我在评论中提到的,将 layout_gravity 添加为 ImageView 的中心。我还编辑了代码,因为您不能在图像视图中使用文本。

    <?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="#ffffff">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="10dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="340dp"
            android:layout_height="340dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_2"/>
    
        <ImageView
            android:layout_width="340dp"
            android:layout_height="340dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_karo"/>
    </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    希望这对你很有效。

    【讨论】:

      【解决方案3】:

      我认为你应该尝试线性布局

      像这样……

      <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:padding="10dp"
      android:orientation="horizontal"
      android:gravity="center_vertical"
      android:layout_gravity="center"
      android:background="#ffffff">
      
      <ImageView
          android:layout_width="340dp"
          android:layout_height="340dp"
          android:layout_gravity="center"
          android:src="@drawable/app_icon"
          android:text="Hello World!"
          android:tint="#ff0000"
          android:layout_weight="1"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.498"
          tools:layout_editor_absoluteX="-52dp" />
      
      <ImageView
          android:layout_width="340dp"
          android:layout_height="340dp"
          android:layout_weight="1"
          android:layout_gravity="center"
          android:src="@drawable/app_icon"
          android:text="Hello World!"
          android:tint="#0000b3"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.498"
          tools:layout_editor_absoluteX="123dp" />
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        我可以看到您的问题,您可以使用约束布局属性在一行中设置两个图像:-

        从头开始

        端到端

        从头到尾

        从头到尾

        例如:-

        <?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="wrap_content"
            android:layout_gravity="center">
        
            <ImageView
                android:id="@+id/button_save"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="4dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/button_share"
                app:layout_constraintHorizontal_chainStyle="spread" />
        
            <ImageView
                android:id="@+id/button_share"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="4dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintStart_toEndOf="@+id/button_save"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent" />
        
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 2014-10-01
          • 2013-08-18
          • 1970-01-01
          • 2018-12-15
          • 1970-01-01
          • 2012-06-29
          • 2013-11-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多