如何在约束布局中设置左右文本视图和单选按钮?

时间:2018-02-09 07:33:38

标签: android

<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="wrap_content">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/phone_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.01"
        android:lineSpacingExtra="32sp"
        android:text="+44 2079460860"
        android:textColor="@color/colorDusk"
        android:textSize="16sp"
        android:textStyle="normal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:lineSpacingExtra="32sp"
        android:text="Mobile UK"
        android:textColor="@color/secondryColor"
        android:textSize="14sp"
        android:textStyle="normal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/phone_number" />

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/radio_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/radio_button_background"
        android:button="@null"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.864"
        app:layout_constraintStart_toEndOf="@+id/phone_number"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

这是我的RecyclerView:

 <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:paddingTop="25dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title" />

这是我的布局我无法向左设置或启动约束布局中的文本视图和右侧或端侧单选按钮我的当前屏幕如下:

以下是截图:

enter image description here

虽然我想要复选框右侧或约束布局的结尾,请建议我实现此目的

2 个答案:

答案 0 :(得分:0)

更新了

 <?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="wrap_content">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/phone_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.01"
            android:lineSpacingExtra="32sp"
            android:text="+44 2079460860"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:textStyle="normal"
            app:layout_constraintEnd_toStartOf="@id/radio_button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:text="Mobile UK"
            android:textColor="@color/colorPrimary"
            android:textSize="14sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/radio_button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/phone_number" />

        <android.support.v7.widget.AppCompatRadioButton
            android:id="@+id/radio_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

尝试以下代码,将android.support.constraint.ConstraintLayout更改为LinearLayout并设置布局weight

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <LinearLayout
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/phone_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.01"
            android:lineSpacingExtra="32sp"
            android:text="+44 2079460860"
            android:textColor="@color/colorPrimary"
            android:textSize="16sp"
            android:textStyle="normal"
            />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:lineSpacingExtra="32sp"
            android:text="Mobile UK"
            android:textColor="@color/colorAccent"
            android:textSize="14sp"
            android:textStyle="normal"
            />



    </LinearLayout>

    <LinearLayout
        android:gravity="right"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatRadioButton
            android:id="@+id/radio_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/ic_launcher"
            android:button="@null"
            />

    </LinearLayout>



</LinearLayout>

或者,您可以使用以下示例代码:

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<LinearLayout
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mobile UK"
        android:textColor="@color/colorAccent"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+44 2079460860"
        android:textColor="@color/colorPrimary"
        />

</LinearLayout>

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

    <RadioButton
        android:id="@+id/radio_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>