Android LinearLayout在实际设备上看起来不一样
我正在为Android应用程序创建一个自定义数字键盘。为此,我使用了几个水平线性布局 以下是与该部分活动相关的代码:Android LinearLayout在实际设备上看起来不一样,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我正在为Android应用程序创建一个自定义数字键盘。为此,我使用了几个水平线性布局 以下是与该部分活动相关的代码: 在这里,您可以看到Android studio中的情况,以及在实际设备上的情况。我不明白为什么会这样。我使用的是dp,据我所知,它应该是动态像素,因此它们在每台设备上都应该具有良好的缩放效果。显然,您在第三行中这样做: android:layout_weight="1" 这会导致问题-如果屏幕不够宽,无法按照宽度指示进行操作,则会指示所有按钮都不相同,因此只有最后一个按钮需要调
在这里,您可以看到Android studio中的情况,以及在实际设备上的情况。我不明白为什么会这样。我使用的是dp,据我所知,它应该是动态像素,因此它们在每台设备上都应该具有良好的缩放效果。显然,您在第三行中这样做:
android:layout_weight="1"
这会导致问题-如果屏幕不够宽,无法按照宽度指示进行操作,则会指示所有按钮都不相同,因此只有最后一个按钮需要调整大小。显然,您会对第三行执行此操作:
android:layout_weight="1"
这会导致问题-如果您的屏幕不够宽,无法按照您的宽度指示操作,则您会指示所有按钮都不相同,因此只有最后的按钮需要调整大小。请用以下内容替换您的4线布局:
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:layout_weight="1"
android:id="@+id/digit1"
android:layout_width="0dip"
android:layout_height="match_parent"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:id="@+id/digit4"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:id="@+id/digit7"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/digit0"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
注意:
如果您想降低正方形的高度,只需将:android:layout\u weight=0.2的值降低到android:layout\u weight=0.15,请将您的4线布局替换为:
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:layout_weight="1"
android:id="@+id/digit1"
android:layout_width="0dip"
android:layout_height="match_parent"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:id="@+id/digit4"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:id="@+id/digit7"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="3"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="0dip">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/digit0"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
注意:
如果你想降低正方形的高度,只需将:android:layout\u weight=0.2的值降低到:android:layout\u weight=0.15,这是你需要的布局,让它工作起来。dp被转换为近似尺寸。但是,如果你想用屏幕大小来表示元素,你可以考虑使用所有的权重。
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="67dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digit1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digit4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digit7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/digit0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
</LinearLayout>
这是你需要的布局,让这件事的工作。dp被转换为近似尺寸。但是,如果你想用屏幕大小来表示元素,你可以考虑使用所有的权重。
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="67dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digit1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digit4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digit7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/digit0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
</LinearLayout>
请检查下面的代码。它可能会解决你的问题。如果您面临任何问题,请在评论部分告诉我 使用您自己的绘图工具和资产
如果答案对您有效,请批准。谢谢 请检查下面的代码。它可能会解决你的问题。如果您面临任何问题,请在评论部分告诉我 使用您自己的绘图工具和资产 如果答案对您有效,请批准。谢谢 试试这个
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digit1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digit4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digit7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/digit0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
</LinearLayout>
试试这个
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digit1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digit4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digit7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="2dp">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/digit0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
</LinearLayout>
您在视图上使用的是固定大小,因为不同的手机具有不同的屏幕大小,这会使您的屏幕不响应 如果您想使用LinearLayout,您可能需要使用android:layout\u weight和android:weightSum属性,以便使视图相对于屏幕具有一定的大小。 您可以这样做,但所有嵌套视图都很可能会影响布局的性能 如果您希望布局能够响应您可以使用的所有屏幕大小,以下是一个示例:
<?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">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="1"
app:layout_constraintBottom_toBottomOf="@+id/button10"
app:layout_constraintEnd_toStartOf="@+id/button10"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button10" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="x"
app:layout_constraintBottom_toBottomOf="@+id/button2"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toTopOf="@+id/button2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="8"
app:layout_constraintBottom_toBottomOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toTopOf="@+id/button4" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="8"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button7" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="7"
app:layout_constraintBottom_toBottomOf="@+id/button4"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button4" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="6"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button7"
app:layout_constraintTop_toTopOf="@+id/button7" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="5"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toStartOf="@+id/button6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button8"
app:layout_constraintTop_toBottomOf="@+id/button10" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="4"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintEnd_toStartOf="@+id/button7"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button7" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="3"
app:layout_constraintBottom_toBottomOf="@+id/button10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button10"
app:layout_constraintTop_toTopOf="@+id/button10" />
<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/button7"
app:layout_constraintEnd_toStartOf="@+id/button9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
</android.support.constraint.ConstraintLayout>
它将如下所示:
您在视图上使用的是固定大小,因为不同的手机具有不同的屏幕大小,这会使您的屏幕不响应 如果您想使用LinearLayout,您可能需要使用android:layout\u weight和android:weightSum属性,以便使视图相对于屏幕具有一定的大小。 您可以这样做,但所有嵌套视图都很可能会影响布局的性能 如果您希望布局能够响应您可以使用的所有屏幕大小,以下是一个示例:
<?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">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="1"
app:layout_constraintBottom_toBottomOf="@+id/button10"
app:layout_constraintEnd_toStartOf="@+id/button10"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button10" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="x"
app:layout_constraintBottom_toBottomOf="@+id/button2"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toTopOf="@+id/button2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="8"
app:layout_constraintBottom_toBottomOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toTopOf="@+id/button4" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="8"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button7" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="7"
app:layout_constraintBottom_toBottomOf="@+id/button4"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button4" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="6"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button7"
app:layout_constraintTop_toTopOf="@+id/button7" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="5"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toStartOf="@+id/button6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button8"
app:layout_constraintTop_toBottomOf="@+id/button10" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="4"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintEnd_toStartOf="@+id/button7"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button7" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="3"
app:layout_constraintBottom_toBottomOf="@+id/button10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button10"
app:layout_constraintTop_toTopOf="@+id/button10" />
<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/button7"
app:layout_constraintEnd_toStartOf="@+id/button9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
</android.support.constraint.ConstraintLayout>
它将如下所示:
尝试使用ConstraintLayout您可以简单地解决此问题只需给出全部宽度=0dp尝试使用ConstraintLayout您可以简单地解决此问题只需给出全部宽度=0dp