Android约束布局中如何均匀分布对象?

我创建了一个数字键盘,输入密码即可进入另一个活动,它工作得很好,但布局很混乱。在我的屏幕截图中,您可以看到它是一个简单的数字键盘,但我的按钮大小为 46dp。当我切换到另一个模拟器或部署到我自己的具有相同尺寸的设备时,它会将按钮放在整个屏幕上。它需要看起来像任何 Android 设备上的屏幕截图,至少在纵向模式下。

https://img1.sycdn.imooc.com/6527992f00017d2c03860807.jpg

我发现类似的问题没有帮助,因为它们使用了线性布局。如果可能的话,我想在约束布局中执行此操作。我来自网络开发背景,所以我通常会使用 % 来处理宽度/高度,但不确定它在 Android 开发中是如何工作的。


我只是在示例代码中放入 1 个按钮,因为除了值和约束关系之外,所有其他按钮几乎都是相同的。


<androidx.constraintlayout.widget.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="#9C8A7C"

    tools:context=".PswdActivity">


<Button

    android:id="@+id/button5"

    android:layout_width="125dp"

    android:layout_height="125dp"

    android:layout_marginStart="24dp"

    android:layout_marginTop="24dp"

    android:layout_marginEnd="24dp"

    android:textSize="50sp"

    android:text="5"

    android:onClick="btnEnterCode"

    app:layout_constraintEnd_toStartOf="@+id/button6"

    app:layout_constraintStart_toEndOf="@+id/button4"

    app:layout_constraintTop_toBottomOf="@+id/button2" />


尚方宝剑之说
浏览 131回答 3
3回答

慕斯王

我认为,网格布局更适合此目的,因为按钮看起来像网格。这将适合任何屏幕。<?xml version="1.0" encoding="utf-8"?><GridLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:padding="20dp"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:columnCount="3"&nbsp; &nbsp; android:rowCount="5"&nbsp; &nbsp; android:background="#fff"&nbsp; &nbsp; >&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"/>&nbsp; &nbsp; <Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnWeight="3"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_rowWeight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_columnSpan="3"&nbsp; &nbsp; &nbsp; &nbsp; /></GridLayout>

桃花长相依

<Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/three"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="60dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="60dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_alignParentTop="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="2dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="4dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="?attr/selectableItemBackgroundBorderless"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:clickable="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:focusable="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/three"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/colorPrimaryDark"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="18sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_column="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@+id/button2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.5"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toEndOf="@+id/two"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/six"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:layout_row="2" />你可以使用它,就像我在 Framelayout 中使用这种类型的视图制作计算器一样,如果你想要完整的 xml 代码,我可以与你分享

有只小跳蛙

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout&nbsp;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"><Button&nbsp; &nbsp; android:id="@+id/button1"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="1"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@id/button2"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button2"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button2"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="2"&nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@id/button5"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button3"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="3"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@id/button2"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button2"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button4"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="4"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@id/button5"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button5"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button5"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="5"&nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@id/button8"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@id/button2"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button6"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="6"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@id/button5"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button5"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button7"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="7"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@id/button8"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button8"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button8"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="8"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button9"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="9"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@id/button8"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button8"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button10"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="X"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@id/button11"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button11"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button11"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="0"&nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@id/button13"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@id/button8"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button12"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="OK"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@id/button11"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="@id/button11"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.3" /><Button&nbsp; &nbsp; android:id="@+id/button13"&nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; android:layout_height="0dp"&nbsp; &nbsp; android:text="back"&nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHeight_percent="0.19"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@id/button11"&nbsp; &nbsp; app:layout_constraintWidth_percent="0.95" />此布局适用于任何屏幕尺寸或方向。纵向:横向方向:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java