我正在为 Android 应用创建自定义数字板。我为此使用了几个水平的 LinearLayout
。
这是与activity
部分相关的代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="67dp">
<ImageView
android:id="@+id/digit1"
android:layout_width="117dp"
android:layout_height="67dp"
app:srcCompat="@drawable/rsz_untitled_3" />
<ImageView
android:id="@+id/digit2"
android:layout_width="118dp"
android:layout_height="67dp"
app:srcCompat="@drawable/digit_2" />
<ImageView
android:id="@+id/digit3"
android:layout_width="100dp"
android:layout_height="67dp"
android:layout_weight="1"
app:srcCompat="@drawable/digit_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<ImageView
android:id="@+id/digit4"
android:layout_width="117dp"
android:layout_height="67dp"
app:srcCompat="@drawable/digit_4" />
<ImageView
android:id="@+id/digit5"
android:layout_width="118dp"
android:layout_height="67dp"
app:srcCompat="@drawable/digit_5" />
<ImageView
android:id="@+id/digit6"
android:layout_width="100dp"
android:layout_height="67dp"
android:layout_weight="1"
app:srcCompat="@drawable/digit_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="@+id/digit7"
android:layout_width="117dp"
android:layout_height="67dp"
app:srcCompat="@drawable/digit_7" />
<ImageView
android:id="@+id/digit8"
android:layout_width="118dp"
android:layout_height="67dp"
app:srcCompat="@drawable/digit_8" />
<ImageView
android:id="@+id/digit9"
android:layout_width="100dp"
android:layout_height="67dp"
android:layout_weight="1"
app:srcCompat="@drawable/digit_9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="@+id/digitvoid"
android:layout_width="117dp"
android:layout_height="67dp" />
<ImageView
android:id="@+id/digit0"
android:layout_width="118dp"
android:layout_height="67dp"
app:srcCompat="@drawable/digit_0" />
<ImageView
android:id="@+id/digitdelete"
android:layout_width="100dp"
android:layout_height="67dp"
android:layout_weight="1"
app:srcCompat="@drawable/digit_delete" />
</LinearLayout>
在这里您可以看到它在 Android studio 中的样子,以及它在实际设备上的样子。我不明白为什么会这样。我正在使用 dp
(根据我的理解应该是动态像素),因此它们应该在每个设备上都能很好地缩放。
最佳答案
您在 View 上使用固定尺寸,因为不同的手机有不同的屏幕尺寸,这会使您的屏幕没有响应。
如果你想使用 LinearLayout
你可能会想要使用 android:layout_weight
和 android:weightSum
属性来给出你的观点一些尺寸相对于你的屏幕。
您可以这样做并且它会起作用,但所有嵌套 View 很可能会影响您的布局性能。
如果你想要一个响应所有屏幕尺寸的布局,你可以使用 ConstraintLayout与 chains ,这里是一个例子:
<?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>
它看起来像这样:
关于Android LinearLayout 在实际设备上看起来不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55840249/