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

时间:2019-04-25 00:29:34

标签: android android-layout android-linearlayout

我正在为Android应用创建自定义数字键盘。为此,我使用了几个水平的flavor1 { dimension "client" if(DEBUG) resValue 'string', 'some_string', 'Debug value for flavor1' buildConfigField 'String', 'SOME_CONSTANT_STRING', '"hola"' manifestPlaceholders = [some_api_key: 'bcb89518a025'] else resValue 'string', 'some_string', 'Release value for flavor1' buildConfigField 'String', 'SOME_CONSTANT_STRING', '"konichiwa"' manifestPlaceholders = [some_api_key: '055a33d49e12'] applicationId 'com.nabenik.flavor1' } flavor2 { dimension "client" if(DEBUG) resValue 'string', 'some_string', 'Debug value for flavor2' buildConfigField 'String', 'SOME_CONSTANT_STRING', '"que onda"' manifestPlaceholders = [some_api_key: 'DEBUG_222222'] else resValue 'string', 'some_string', 'Release value for flavor2' buildConfigField 'String', 'SOME_CONSTANT_STRING', '"quiubo"' manifestPlaceholders = [some_api_key: 'RELEASE_2222'] applicationId 'com.nabenik.flavor2' }

以下是与LinearLayout的那部分相关的代码:

activity

enter image description here

enter image description here

在这里您可以看到在 Android Studio 中的外观以及在实际设备上的外观。我不明白为什么会这样。我正在使用<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> (据我了解应该是动态像素),因此它们应该在每个设备上都可以很好地缩放。

6 个答案:

答案 0 :(得分:1)

很显然,您是在第三行中这样做的:

android:layout_weight="1"

这会引起问题-如果您的屏幕宽度不足以按照宽度指示操作,则会指示所有按钮都不相同,因此仅应调整最后一个按钮的大小。

答案 1 :(得分:1)

这是使此功能正常运行所需的布局。将dp转换为近似大小。但是,如果要使用屏幕尺寸对元素进行比例分配,则可以考虑一直使用weight

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

答案 2 :(得分:1)

您在视图上使用的是固定尺寸,因为不同的手机具有不同的屏幕尺寸,这会使您的屏幕无响应。

如果您想使用LinearLayout,则可能需要使用android:layout_weightandroid:weightSum属性,以使视图相对于屏幕具有一定的大小。 您可以执行此操作,并且它可以工作,但是所有嵌套视图都很可能会影响布局性能。

如果您希望布局能够响应所有屏幕尺寸,则可以将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

答案 3 :(得分:0)

请用以下内容替换您的4 UAInstanceIDListenerService

LinearLayout

并添加父布局这两个属性:

  <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:orientation="vertical" android:weightSum="1.0" 的值减小为android:layout_weight="0.2"

答案 4 :(得分:0)

请检查以下代码。它可以解决您的问题。如果您遇到任何问题,请在评论部分告诉我。

使用自己的绘画和资产。

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

        <ImageView
            android:id="@+id/digit1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

        <ImageView
            android:id="@+id/digit2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

        <ImageView
            android:id="@+id/digit3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="67dp">

        <ImageView
            android:id="@+id/digit4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

        <ImageView
            android:id="@+id/digit5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

        <ImageView
            android:id="@+id/digit6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="67dp">

        <ImageView
            android:id="@+id/digit7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

        <ImageView
            android:id="@+id/digit8"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

        <ImageView
            android:id="@+id/digit9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="67dp">

        <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/add_more_icon" />

        <ImageView
            android:id="@+id/digitdelete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/add_more_icon" />

    </LinearLayout>
</LinearLayout>

如果它适合您,请批准答案。谢谢!

答案 5 :(得分:0)

尝试

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