Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Android ConstraintLayout使视图居中时出现问题_Android_User Interface_Layout_Constraints_Center - Fatal编程技术网

使用Android ConstraintLayout使视图居中时出现问题

使用Android ConstraintLayout使视图居中时出现问题,android,user-interface,layout,constraints,center,Android,User Interface,Layout,Constraints,Center,我对Android中的ConstraintLayout有点问题 请注意下图: 我想提请您注意中间有三个视图:TextView和两个按钮 垂直虚线和水平虚线是固定的指导线(以屏幕高度/宽度的百分比表示),用于将这些视图约束到 如您所见,TextView在所有4个方向上都受到约束 最右侧的按钮在三个方向上受约束:顶部、底部和右侧。。。并且具有1:1的纵横比约束 现在,中间的按钮仅限于顶部和底部,并且还具有1∶1的纵横比。 到目前为止一切都很好。。。但是,如果我们想保持所有东西都完全相同,除了中间的

我对Android中的ConstraintLayout有点问题

请注意下图:

我想提请您注意中间有三个视图:TextView和两个按钮

垂直虚线和水平虚线是固定的指导线(以屏幕高度/宽度的百分比表示),用于将这些视图约束到

如您所见,TextView在所有4个方向上都受到约束

最右侧的按钮在三个方向上受约束:顶部、底部和右侧。。。并且具有1:1的纵横比约束

现在,中间的按钮仅限于顶部和底部,并且还具有1∶1的纵横比。

到目前为止一切都很好。。。但是,如果我们想保持所有东西都完全相同,除了中间的按钮居中,这样它的左右空间就相等了,那该怎么办呢

有人可能会认为,简单地将水平约束应用于其最近的邻居就可以了。。。但是没有

结果是:

所以。。。由于某种原因,按钮的大小正在增大。为什么?

在我看来,Android Studio应用的是1:1纵横比规则(在本例中),即“高度是宽度的函数…”即首先计算宽度(基于新应用的约束),因此优先

如果它只是以另一种方式,说“宽度=高度”,高度优先。。。(与应用这些最终约束之前的做法相同),然后一切都会好起来

所以。。。换言之,如何将“中间”视图居中于其他两个“右”和“左”视图之间,并且仍然保持:

1) 所有三个视图都具有匹配的高度,因为它们受相同的水平约束约束约束约束

2) 左视图处于固定位置

3) 右视图的纵横比为1:1,不允许与其右侧基准线(固定位置)分离

?

编辑:我为任何想要使用它的人制作了以下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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guidelineH1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineH2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.50" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        android:gravity="center_vertical"
        android:text="1"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@+id/guidelineH2"
        app:layout_constraintEnd_toStartOf="@+id/guidelineV1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineH1" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="3"
        app:layout_constraintBottom_toTopOf="@+id/guidelineH2"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineH1" />

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guidelineH2"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/guidelineH1">

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>


你的问题很清楚,可以理解,我不知道为什么人们一直投票否决你的问题。我想他们有压力了?也许。。。我觉得这很有趣,因为他们不知道答案。谢谢你的尝试,但在有横向指导原则和1:1高宽比的特定情况下,这似乎不起作用。是的。我以前做过。你能分享你的部分布局代码吗?我会帮你更正好的,我会编辑这个问题,包括一个代码示例。是的,这很有效!我没有想到要再筑巢。
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guidelineH1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineH2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.50" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        android:gravity="center_vertical"
        android:text="1"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@+id/guidelineH2"
        app:layout_constraintEnd_toStartOf="@+id/guidelineV1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineH1" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="3"
        app:layout_constraintBottom_toTopOf="@+id/guidelineH2"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guidelineH1" />

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guidelineH2"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/guidelineH1">

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>