2

我正在尝试制作一个 ConstraintLayout 来用相对和线性布局替换常规布局,但是在卡片视图中垂直居中两个视图时遇到了一些麻烦。

下面的布局文件是我要替换的当前布局。

在此处输入图像描述

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/main_button_side_margin"
    android:layout_marginStart="@dimen/main_button_side_margin"
    android:layout_marginTop="@dimen/main_button_top_margin"
    android:paddingBottom="2dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="2dp">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/select_language_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="@android:color/transparent"
        card_view:cardCornerRadius="0dp"
        card_view:cardElevation="0dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/language_stroke"
            android:minHeight="80dp">

            <ImageView
                android:id="@+id/img_language"
                android:layout_width="@dimen/main_button_size"
                android:layout_height="@dimen/main_button_size"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/main_button_icon_margin"
                android:layout_marginStart="@dimen/main_button_icon_margin"
                android:src="@drawable/ic_language_white_48dp"
                android:tint="@color/language_color" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/menu_text_margin"
                android:layout_marginLeft="@dimen/menu_text_margin"
                android:layout_marginRight="@dimen/menu_text_margin"
                android:layout_marginStart="@dimen/menu_text_margin"
                android:layout_toEndOf="@id/img_language"
                android:layout_centerVertical="true"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/main_button_text_title_margin"
                    android:text="Text" />

                <TextView
                    android:fontFamily="sec-roboto-light"
                    android:gravity="start"
                    android:id="@+id/language_desc"
                    android:text="description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>

我目前的结果如下:

在此处输入图像描述

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.CardView
        android:id="@+id/select_language_button"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardCornerRadius="0dp"
        app:cardElevation="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/language_stroke">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:text="desc"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="@+id/textView1"
                app:layout_constraintTop_toBottomOf="@+id/textView1" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="Text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/img_language"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/img_language"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="12dp"
                android:layout_marginStart="12dp"
                android:layout_marginTop="8dp"
                android:src="@drawable/ic_language_white_48dp"
                android:tint="@color/language_color"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

问题是将 textView + textView1 居中在cardview中。我只得到 textView 居中和 textView1 下面。

我已经尝试过“垂直包装”然后“垂直居中”两者,但是当它在卡片视图中为 android:layout_centerVertical="true" 时,我没有得到 LinearLayout(保存两个文本视图)实现的结果。

我想用可视化编辑器来代替更改 xml。

我知道实现它的方法是使用链,但我无法在卡片视图中使用布局编辑来完成它。

有人可以帮助一些屏幕截图/屏幕录像机吗?

4

1 回答 1

4

是的,您必须为您的 textView 和 textView1 使用垂直打包链,以使它们在您的 CardView 中居中。

要在布局编辑器中添加链,您应该同时选择 textView 和 textView1,右键单击它们并选择“垂直居中”

在布局编辑器中添加链

要将链样式更改为打包,您应该点击“链”图标,直到选择打包样式

在布局编辑器中更改链样式

使用 ConstraintLayout 构建响应式 UI - 使用链控制线性组包含一个视频,展示如何在布局编辑器中添加链并更改其样式。

针对您的特定情况的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/select_language_button"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardCornerRadius="0dp"
        app:cardElevation="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/language_stroke">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="0dp"
                android:text="desc"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="@+id/textView1"
                app:layout_constraintTop_toBottomOf="@+id/textView1" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="Text"
                app:layout_constraintBottom_toTopOf="@+id/textView"
                app:layout_constraintLeft_toRightOf="@+id/img_language"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="packed" />

            <ImageView
                android:id="@+id/img_language"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="12dp"
                android:layout_marginStart="12dp"
                android:layout_marginTop="8dp"
                android:src="@drawable/ic_language_white_48dp"
                android:tint="@color/language_color"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>
于 2017-10-31T09:00:47.103 回答