我如何使用ConstraintLayout将图标放在中间的一些图标旁边

时间:2019-06-11 23:08:02

标签: android android-constraintlayout

我尝试将Icon放在中间,但是我失败了,有人可以留下可以解决该问题的name属性吗?

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="@color/SSA1Background"
    tools:context=".SplashScreen">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/txtSignInVia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="@font/main_font_file"
            android:text="@string/SSA1SignInVia"
            android:textColor="@color/SSA1SignInText"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btnFacebook"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/s_s_a_1_facebook_btn"
            android:fontFamily="@font/icons_brands"
            android:text="@string/SSA1FacebookIcon"
            android:textColor="@color/SSA1FacebookText"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />

        <Button
            android:id="@+id/btnGoogle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/s_s_a_1_google_btn"
            android:fontFamily="@font/icons_brands"
            android:text="@string/SSA1GoogleIcon"
            android:textColor="@color/SSA1GoogleText"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

电话结果

enter image description here

Facebook和Google图标,我希望它们位于中心。

2 个答案:

答案 0 :(得分:0)

将视图的两端(左/右)相互约束,并将水平链样式设置为压缩,如下所示:

  • 也..不需要将约束布局嵌套在另一个约束布局中。删除未使用的父约束布局

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <TextView
            android:id="@+id/txtSignInVia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="@font/main_font_file"
            android:text="@string/SSA1SignInVia"
            android:textColor="@color/SSA1SignInText"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btnFacebook"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/s_s_a_1_facebook_btn"
            android:fontFamily="@font/icons_brands"
            android:text="@string/SSA1FacebookIcon"
            android:textColor="@color/SSA1FacebookText"
            android:textSize="20sp"
            app:layout_constraintEnd_toStartOf="@id/btnGoogle"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />
    
        <Button
            android:id="@+id/btnGoogle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="5dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/s_s_a_1_google_btn"
            android:fontFamily="@font/icons_brands"
            android:text="@string/SSA1GoogleIcon"
            app:layout_constraintStart_toEndOf="@id/btnFacebook"
            app:layout_constraintHorizontal_chainStyle="packed"
            android:textColor="@color/SSA1GoogleText"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtSignInVia" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

答案 1 :(得分:0)

对于button ID btnFacebook,添加

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"`

对于button ID btnGoogle,添加

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.6"` 

因为如果未设置所有约束,则视图将不在我们想要的位置。我希望这有帮助。