【问题标题】:How do I get Buttons to adapt to any screen size in Android Studio如何让按钮适应 Android Studio 中的任何屏幕尺寸
【发布时间】:2020-09-06 03:36:24
【问题描述】:

所以我想要一个 ImageView,它是一个语音气泡和一个按钮,它是我活动中的企鹅。 penguinbutton 应该在右下角,speechbubble ImageView 应该在企鹅上方的左边一点点。 当我将所有宽度和高度设置为固定值时,它工作正常,但是一旦我在另一台设备上打开它,它就会全部调整大小。 我的问题是我找不到任何方法让这些元素按照我想要的方式适应不同的屏幕尺寸。 下面是一些代码,它确实在每个设备上调整了它们的大小,但它调整了 50/50 的大小,因此 ImageView 占据了上屏的 50%,而按钮占据了下屏的 50%。 我不知道如何在不同屏幕上调整大小的同时将它们保持在某个位置。 欢迎任何帮助。

   <LinearLayout
    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:orientation="vertical"
    android:background="@drawable/ein_hintergrund2"
    tools:context=".MainActivity">
<ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:srcCompat="@drawable/speechbubble"
        android:id="@+id/klick_textView"
        android:layout_marginBottom="9dp"
        android:contentDescription="@string/todo"/>
<Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/start_button"
        android:background="@drawable/pinguin"/> </LinearLayout>

【问题讨论】:

    标签: java android android-studio button


    【解决方案1】:

    由于它有点复杂,我会根据不同的屏幕尺寸使用不同的布局。或者如果是风景等。

    我建议你看看this page

    【讨论】:

      【解决方案2】:

      您应该查看ConstraintLayout,它可以帮助您在大多数情况下根据设备/屏幕设置类似的布局。

      您可以找到codelab 来帮助您完成它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        • 2019-07-12
        • 1970-01-01
        相关资源
        最近更新 更多