带有权重的android圆形按钮数字键盘

时间:2018-07-23 04:53:45

标签: android android-layout

我正在尝试使动态数字键盘视图具有圆形形状,但是问题是,当我分配背景时,它不能正确显示我正在使用权重,因此它可以在所有屏幕尺寸和方向上工作,如何才能实现这是我的布局< / p>

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/pin_code_first_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <Button
            android:id="@+id/pin_code_button_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/rounded"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

    </LinearLayout>

    <LinearLayout android:id="@+id/pin_code_second_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/pin_code_first_row"
        android:weightSum="3">

        <Button
            android:id="@+id/pin_code_button_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

    </LinearLayout>

    <LinearLayout android:id="@+id/pin_code_third_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/pin_code_second_row"
        android:weightSum="3">

        <Button
            android:id="@+id/pin_code_button_7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

    </LinearLayout>

    <LinearLayout android:id="@+id/pin_code_fourth_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/pin_code_third_row"
        android:weightSum="3">

        <Button
            android:id="@+id/pin_code_button_10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/pin_code_button_clear"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

    </LinearLayout>

</LinearLayout>

这是我的圆形

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

    <!-- fill color -->
    <solid android:color="@color/colorAccent" />

    <!-- radius -->
    <stroke
        android:width="1dp"
        android:color="@color/colorAccent" />

    <!-- corners -->
    <corners
        android:radius="2dp"/>
</shape>

mylayout

但是我要使布局看起来像这样

enter image description here

如何在不使用静态间距和权重的情况下进行此布局,请帮助我做错了什么?

5 个答案:

答案 0 :(得分:4)

使用ConstraintLayout如下:

这是您的代码: your_layout.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:background="#75A5CB"
    android:padding="40dp">

    <TextView
        android:id="@+id/tvEnterCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="Enter Access Code"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:background="#ffffff"
        android:gravity="center"
        android:minWidth="100dp"
        android:padding="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvEnterCode"
        tools:text="..." />

    <Button
        android:id="@+id/pin_code_button_1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/shape_circle_blue"
        android:text="1"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvCode" />

    <Button
        android:id="@+id/pin_code_button_2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="2"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_1"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_1"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_1" />

    <Button
        android:id="@+id/pin_code_button_3"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="3"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_2"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_1" />

    <Button
        android:id="@+id/pin_code_button_4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="@dimen/_16sdp"
        android:background="@drawable/shape_circle_blue"
        android:text="4"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_1" />

    <Button
        android:id="@+id/pin_code_button_5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="5"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_4"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_6"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_1"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_4" />

    <Button
        android:id="@+id/pin_code_button_6"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="6"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_5"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_4" />

    <Button
        android:id="@+id/pin_code_button_7"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="@dimen/_16sdp"
        android:background="@drawable/shape_circle_blue"
        android:text="7"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_8"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_4" />

    <Button
        android:id="@+id/pin_code_button_8"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="8"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_7"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_9"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_4"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_7" />

    <Button
        android:id="@+id/pin_code_button_9"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="9"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_8"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_7" />

    <Button
        android:id="@+id/pin_code_button_0"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="@dimen/_16sdp"
        android:background="@drawable/shape_circle_blue"
        android:text="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toEndOf="@+id/pin_code_button_8"
        app:layout_constraintStart_toStartOf="@+id/pin_code_button_8"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_8" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="Delete"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_0" />
</android.support.constraint.ConstraintLayout>

可绘制形状: your_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#4588C0" />
    <stroke
        android:width="2dp"
        android:color="#ffffff" />
</shape>

如果要以白色显示按下/选中的按钮,则必须创建可绘制的选择器并将其提供给按钮背景。

Layout Screenshot

编辑:如果您不想提供静态垂直边距,请使用以下代码

<?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:background="#75A5CB"
    android:padding="40dp">

    <TextView
        android:id="@+id/tvEnterCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="Enter Access Code"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:background="#ffffff"
        android:gravity="center"
        android:minWidth="100dp"
        android:padding="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvEnterCode"
        tools:text="..." />

    <Button
        android:id="@+id/pin_code_button_1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/shape_circle_blue"
        android:text="1"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvCode" />

    <Button
        android:id="@+id/pin_code_button_2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="2"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_1"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_1"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_1" />

    <Button
        android:id="@+id/pin_code_button_3"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="3"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_2"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_1" />

    <Button
        android:id="@+id/pin_code_button_4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="4"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toTopOf="@+id/pin_code_button_7"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_1" />

    <Button
        android:id="@+id/pin_code_button_5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="5"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_4"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_6"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_1"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_4" />

    <Button
        android:id="@+id/pin_code_button_6"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="6"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_5"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_4" />

    <Button
        android:id="@+id/pin_code_button_7"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="7"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toTopOf="@+id/pin_code_button_0"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_8"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_4" />

    <Button
        android:id="@+id/pin_code_button_8"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="8"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_7"
        app:layout_constraintEnd_toStartOf="@+id/pin_code_button_9"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_4"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_7" />

    <Button
        android:id="@+id/pin_code_button_9"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="9"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="@+id/pin_code_button_7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/pin_code_button_8"
        app:layout_constraintTop_toTopOf="@+id/pin_code_button_7" />

    <Button
        android:id="@+id/pin_code_button_0"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/shape_circle_blue"
        android:text="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toTopOf="@+id/tvDelete"
        app:layout_constraintEnd_toEndOf="@+id/pin_code_button_8"
        app:layout_constraintStart_toStartOf="@+id/pin_code_button_8"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_8" />

    <TextView
        android:id="@+id/tvDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="Delete"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pin_code_button_0" />
</android.support.constraint.ConstraintLayout>

答案 1 :(得分:2)

以此更改背景可绘制背景

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<!-- fill color -->
<solid android:color="@color/colorAccent"/>

<!-- radius -->
<stroke
    android:width="1dp"
    android:color="@color/colorAccent"/>

<size
    android:width="50dp"
    android:height="50dp"/>
<!-- corners -->
<corners
    android:radius="2dp"/>

根据需要更改尺寸, 您可以使用约束布局或其他方法来实现这种设计

为你@Mateen Chaudhry

<?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:layout_margin="@dimen/margin_30">

<Button
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_white_rounded_corners"
    android:text="1"
    android:textColor="@color/body_text_color"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>


<Button
    android:id="@+id/two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_white_rounded_corners"
    android:text="2"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toStartOf="@+id/three"
    app:layout_constraintStart_toEndOf="@+id/one"/>


<Button
    android:id="@+id/three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_white_rounded_corners"
    android:text="3"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toEndOf="parent"/>

<Button
    android:id="@+id/four"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:text="4"
    android:textColor="@color/body_text_color"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/one"/>


<Button
    android:id="@+id/five"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:text="5"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toStartOf="@+id/three"
    app:layout_constraintStart_toEndOf="@+id/one"
    app:layout_constraintTop_toBottomOf="@+id/one"/>


<Button
    android:id="@+id/six"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:text="6"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/one"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:text="7"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/four"/>

一种替代解决方案是使用textview代替按钮

<?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:layout_margin="@dimen/margin_30">

<TextView
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="1"
    android:textColor="@color/body_text_color"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>


<TextView
    android:id="@+id/two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="2"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toStartOf="@+id/three"
    app:layout_constraintStart_toEndOf="@+id/one"/>


<TextView
    android:id="@+id/three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="3"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toEndOf="parent"/>

<TextView
    android:id="@+id/four"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="4"
    android:textColor="@color/body_text_color"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/one"/>


<TextView
    android:id="@+id/five"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="5"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toStartOf="@+id/three"
    app:layout_constraintStart_toEndOf="@+id/one"
    app:layout_constraintTop_toBottomOf="@+id/one"/>


<TextView
    android:id="@+id/six"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="6"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/one"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_10"
    android:background="@drawable/bg_white_rounded_corners"
    android:gravity="center"
    android:text="7"
    android:textColor="@color/body_text_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/four"/>
 </android.support.constraint.ConstraintLayout>

答案 2 :(得分:2)

首先,我会告诉您使用最新的约束布局,它在设计中效率更高。 @Viraj Patel回答他使用约束布局。

但是您想要在相对布局中实现相同的设计。这很容易。看看这个

第1步
首先创建中心按钮,使其垂直和水平居中。您只需要将此行添加到中心按钮即可。

  

android:layout_centerInParent =“ true”

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/btn_digit5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="5"
        android:textColor="@android:color/white"
        android:background="@drawable/circle"
        android:layout_centerInParent="true"/>

</RelativeLayout>

Am添加了静态宽度和高度以使其成为背景圆。如果要使用WRAP_CONTENTMATCH_PARENT来制作按钮圈,请在运行时用Java代码完成。这里正在使用静态值。

以上xml代码将为您提供这样的功能
enter image description here

第2步
然后,您可以根据中心按钮和另外两个属性值添加更多按钮。

  

android:layout_centerHorizo​​ntal =“ true”
  android:layout_centerVertical =“ true”

现在,我添加Digit4Digit6,它们与Digit5垂直对齐。下面的代码将为您提供结果

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/btn_digit5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="5"
        android:textColor="@android:color/white"
        android:background="@drawable/circle"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_digit4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toLeftOf="@+id/btn_digit5"
        android:layout_marginRight="20dp"
        android:text="4"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit6"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toRightOf="@+id/btn_digit5"
        android:layout_marginLeft="20dp"
        android:text="6"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />  

</RelativeLayout>

向此按钮添加to_leftofto_rightof属性并垂直对齐该属性,并同时添加边距值。

以上代码将为您提供
enter image description here

第3步
现在,我添加了Digit2Digit8,它们与Digit5水平对齐。下面的代码将为您提供结果

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/btn_digit5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="5"
        android:textColor="@android:color/white"
        android:background="@drawable/circle"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_digit2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_above="@+id/btn_digit5"
        android:layout_marginBottom="20dp"
        android:text="2"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toLeftOf="@+id/btn_digit5"
        android:layout_marginRight="20dp"
        android:text="4"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit6"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toRightOf="@+id/btn_digit5"
        android:layout_marginLeft="20dp"
        android:text="6"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit8"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="8"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/btn_digit5"
        android:textColor="@android:color/white" />

</RelativeLayout>

在此按钮上添加layout_belowlayout_above属性和水平对齐属性,并同时添加边距值。

以上代码将为您提供
enter image description here

第4步 借助相对布局属性to_leftof to_right_of layout_above layout_below添加边角数字。看看这个

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/btn_digit5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="5"
        android:textColor="@android:color/white"
        android:background="@drawable/circle"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_digit2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_above="@+id/btn_digit5"
        android:layout_marginBottom="20dp"
        android:text="2"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toLeftOf="@+id/btn_digit5"
        android:layout_marginRight="20dp"
        android:text="4"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit6"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toRightOf="@+id/btn_digit5"
        android:layout_marginLeft="20dp"
        android:text="6"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit8"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="8"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/btn_digit5"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="1"
        android:layout_above="@+id/btn_digit4"
        android:layout_toLeftOf="@+id/btn_digit2"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit3"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="3"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"
        android:layout_toRightOf="@+id/btn_digit2"
        android:layout_above="@+id/btn_digit6"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit7"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="7"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/btn_digit4"
        android:layout_toLeftOf="@id/btn_digit8"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit9"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="9"
        android:layout_below="@+id/btn_digit6"
        android:layout_toRightOf="@id/btn_digit8"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:textColor="@android:color/white" />

</RelativeLayout>

您的输出是
enter image description here

第4步
最后还要加上零。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/btn_digit5"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="5"
        android:textColor="@android:color/white"
        android:background="@drawable/circle"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_digit2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_above="@+id/btn_digit5"
        android:layout_marginBottom="20dp"
        android:text="2"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toLeftOf="@+id/btn_digit5"
        android:layout_marginRight="20dp"
        android:text="4"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit6"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:layout_toRightOf="@+id/btn_digit5"
        android:layout_marginLeft="20dp"
        android:text="6"
        android:layout_centerVertical="true"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit8"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="8"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/btn_digit5"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="1"
        android:layout_above="@+id/btn_digit4"
        android:layout_toLeftOf="@+id/btn_digit2"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit3"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="3"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"
        android:layout_toRightOf="@+id/btn_digit2"
        android:layout_above="@+id/btn_digit6"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit7"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="7"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/btn_digit4"
        android:layout_toLeftOf="@id/btn_digit8"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit9"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="9"
        android:layout_below="@+id/btn_digit6"
        android:layout_toRightOf="@id/btn_digit8"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_digit0"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:background="@drawable/circle"
        android:text="0"
        android:layout_below="@+id/btn_digit8"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textColor="@android:color/white" />

</RelativeLayout>

最终输出

enter image description here

已添加
这将使布局在所有设备尺寸上居中,并在横向模式下居中。

shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<corners android:radius="30dp"/>
<stroke android:color="#FFFFFF"
    android:width="2dp"/>
<solid android:color="@color/colorPrimary"/>

已更新

您需要使用ConstraintLayout。看看这个

<?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=".MainActivity">

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintBottom_toBottomOf="@+id/guideline2"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintStart_toEndOf="@+id/guideline3"
    app:layout_constraintTop_toTopOf="@+id/guideline">

    <Button
        android:id="@+id/btn_digit5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="5"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit8"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit6"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit4"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit2" />

    <Button
        android:id="@+id/btn_digit2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="2"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit5"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit1"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_digit4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="4"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit7"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit1" />

    <Button
        android:id="@+id/btn_digit6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="6"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit5"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit3" />

    <Button
        android:id="@+id/btn_digit8"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="8"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit0"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit9"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit7"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit5" />

    <Button
        android:id="@+id/btn_digit1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="1"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit4"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_digit3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="3"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit2"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_digit7"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="7"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit0_Left"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit8"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit4" />

    <Button
        android:id="@+id/btn_digit9"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/btn_digit6"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="9"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_digit0_right"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit8"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit6" />

    <Button
        android:id="@+id/btn_digit0"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:text="0"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit0_right"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit0_Left"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit8" />

    <Button
        android:id="@+id/btn_digit0_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:visibility="invisible"
        android:text="0"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_digit0"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit9" />

    <Button
        android:id="@+id/btn_digit0_Left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/circle"
        android:visibility="invisible"
        android:text="0"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_digit0"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_digit7" />


</android.support.constraint.ConstraintLayout>

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

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

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

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

5英寸显示屏上的输出
enter image description here

答案 3 :(得分:0)

我认为您可以对此进行处理:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="1"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="2"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="3"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="4"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="5"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="6"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="7"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="8"/>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="9"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="0"/>
    </LinearLayout>
</LinearLayout>

结果如下:

enter image description here

如果您使用“ dp” 进行边距和填充,则使用不同的设备和屏幕尺寸也不会出现任何问题。

答案 4 :(得分:0)

当您要创建表格布局时,最好使用表格布局。在表格布局中,处理重量非常容易。另一点是您必须重视按钮容器的布局。不适用于按钮本身。

<TableRow android:layout_margin="@dimen/margin_half">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="1"/>

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="2"/>

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="3"/>

    </FrameLayout>

</TableRow>

<TableRow android:layout_margin="@dimen/margin_half">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_4"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="4"/>

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_5"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="5"/>

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_6"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="6"/>

    </FrameLayout>

</TableRow>

<TableRow android:layout_margin="@dimen/margin_half">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_7"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="7"/>

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_8"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="8"/>

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:id="@+id/pin_code_button_9"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/circle_back"
            android:text="9"/>

    </FrameLayout>

</TableRow>

这是最终视图,您可以更改距离和边距并获得所需的布局: enter image description here

希望有帮助!