【问题标题】:Relative layout : align view horizontal and vertical center to other views相对布局:将视图水平和垂直中心与其他视图对齐
【发布时间】:2019-04-10 11:20:24
【问题描述】:

我想创建如下布局 点与上面的文本居中对齐的位置。两个点之间有一条线

我试过了

<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Place Order" />

        <TextView android:text="Go to Collect"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView android:text="Collect Order"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView android:src="@drawable/dot_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView android:src="@drawable/line_one"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView android:src="@drawable/dot_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView android:src="@drawable/line_two"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView android:src="@drawable/dot_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

这使点的开头与文本的开头完全相同。而不是居中对齐

我想让点中心与文本对齐。对RelativeLayout 进行了同样的尝试。但我又面临同样的问题

【问题讨论】:

    标签: android android-linearlayout vertical-alignment android-constraintlayout android-relativelayout


    【解决方案1】:

    最好的方法是使用ConstraintLayout

    将其添加到您的应用模块依赖项中:

    dependencies {
        [...]
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    }
    

    然后创建例如文本部分元素的线性布局,每个元素的权重为 1。 然后对于下部,使用具有 33% 和 66% 的指引的 ContraintLayout,并使用约束在点之间放置线条。你的代码应该看起来像这样:

    <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=".MainActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Place Order" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Go to Collect" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Collect Order" />
    </LinearLayout>
    
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.33" />
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline66"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.66" />
    
        <ImageView
            android:id="@+id/dot1"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:src="@drawable/circle_orange"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="@+id/guideline33" />
    
        <ImageView
            android:id="@+id/dot2"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:src="@drawable/circle_orange"
            app:layout_constraintLeft_toLeftOf="@+id/guideline33"
            app:layout_constraintRight_toRightOf="@+id/guideline66" />
    
        <ImageView
            android:id="@+id/dot3"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:src="@drawable/circle_orange"
            app:layout_constraintLeft_toLeftOf="@+id/guideline66"
            app:layout_constraintRight_toRightOf="parent" />
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="4dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/square_orange"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/dot1"
            app:layout_constraintRight_toLeftOf="@id/dot2"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="4dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/square_orange"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/dot2"
            app:layout_constraintRight_toLeftOf="@id/dot3"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    Dot 和 Square 是形状可绘制对象:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#FF9800" />
    </shape>
    

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid android:color="@color/hek_orange" />
    </shape>
    

    这给了你这个:

    您可以自行决定使用边距,以便在顶部和底部以及点和线之间创建足够的空间。

    【讨论】:

    • 谢谢。这帮助很大。
    【解决方案2】:

    您应该使用 ConstraintLayout。在 ConstraintLayout 中,您将点指定为

    constraint_leftToLeftOf = R.id.text
    constraint_rightToRightOf = R.id.text
    

    然后它与文本的中心对齐。

    约束布局: https://developer.android.com/training/constraint-layout/

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 2018-06-23
      • 2013-04-25
      • 1970-01-01
      • 2016-11-25
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多