Android LinearLayout 在实际设备上看起来不一样

标签 android android-layout android-linearlayout

我正在为 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>

enter image description here

enter image description here

在这里您可以看到它在 Android studio 中的样子,以及它在实际设备上的样子。我不明白为什么会这样。我正在使用 dp(根据我的理解应该是动态像素),因此它们应该在每个设备上都能很好地缩放。

最佳答案

您在 View 上使用固定尺寸,因为不同的手机有不同的屏幕尺寸,这会使您的屏幕没有响应。

如果你想使用 LinearLayout 你可能会想要使用 android:layout_weightandroid:weightSum 属性来给出你的观点一些尺寸相对于你的屏幕。 您可以这样做并且它会起作用,但所有嵌套 View 很可能会影响您的布局性能。

如果你想要一个响应所有屏幕尺寸的布局,你可以使用 ConstraintLayoutchains ,这里是一个例子:

<?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>

它看起来像这样:

enter image description here

关于Android LinearLayout 在实际设备上看起来不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55840249/

相关文章:

android - 尽管尝试了教程,但蓝牙设备未连接到 Android 应用程序

android - 在我的案例中,操作栏自定义样式

android - 如何向 Android Activity 添加底部菜单

android - FrameLayout 和屏幕百分比

android - 如何使布局中间的小部件拉伸(stretch)以填充Android中的父级

java - 安卓 & 蓝牙 & Arduino

android - 在 Android Horizo​​ntalListView 中刷新 View

android - tabwidget 图标对齐文本

android - 相对布局 : can't use layout_below with link to a LinearLayout child?

android - 以编程方式创建的编辑文本的文本输入布局