ConstraintLayout - 视图寻呼机的宽度

时间:2017-12-22 11:27:19

标签: android android-viewpager width android-constraintlayout

我想在XML文件中将一个视图寻呼机的layout_width(宽度)设置为等于另一个(或两者的开始和结束对齐方式)。

我尝试了以下代码,但似乎无效。

<?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"
    tools:context="com.example.xxx.yyy.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vpTop"
        android:layout_width="@+id/vpCenter"  // Here trying to assign width equal to orange view
        android:layout_height="40dp"
        android:layout_marginBottom="20dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toTopOf="@+id/vpCenter"
        ></android.support.v4.view.ViewPager>

    <android.support.v4.view.ViewPager
        android:id="@+id/vpCenter"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vpBottom"
        android:layout_width="@+id/vpCenter" // Here trying to assign width equal to orange view
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintTop_toBottomOf="@+id/vpCenter"
        ></android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

这是预览:

enter image description here

我希望绿色视图寻呼机的宽度等于橙色视图寻呼机。 注意:橙色视图寻呼机已修复layout_width = 200.我不想为绿色视图寻呼机设置固定宽度(200),但我希望将橙色视图的宽度分配给两个绿色视图。 (开始和结束对齐应该等于)

我想要这样的事情:

enter image description here

怎么办?

2 个答案:

答案 0 :(得分:2)

我做了一件你想要的事情:

<?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.v4.view.ViewPager
        android:id="@+id/vpTop"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toTopOf="@+id/vpCenter"
        app:layout_constraintEnd_toEndOf="@id/vpCenter"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/vpCenter"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vpCenter"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vpBottom"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintEnd_toEndOf="@+id/vpCenter"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/vpCenter" <!--key attributes-->
        app:layout_constraintTop_toBottomOf="@+id/vpCenter" />

</android.support.constraint.ConstraintLayout>


这个xml的输出是:
output

答案 1 :(得分:1)

试试这个

<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"
    tools:context="com.example.xxx.yyy.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vpTop"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toTopOf="@+id/vpCenter"
        app:layout_constraintEnd_toEndOf="@id/vpCenter"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/vpCenter"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vpCenter"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vpBottom"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintEnd_toEndOf="@+id/vpCenter"
        app:layout_constraintStart_toStartOf="@+id/vpCenter"
        app:layout_constraintTop_toBottomOf="@+id/vpCenter" />


</android.support.constraint.ConstraintLayout>

<强>输出

enter image description here