LinearLayout 在小型设备中没有响应

MTA*_*MTA 5 java android android-layout kotlin android-studio

我的 Android 应用程序中有一个布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/player_background"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".Fragments.PlayerFragment">

    <ProgressBar
        android:id="@+id/progress_bar_player"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:theme="@style/ProgressBarStyle"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/title_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Title"
        android:textColor="@color/white"
        android:textSize="24dp" />

    <ImageView
        android:id="@+id/view_imageview"
        android:layout_width="280dp"
        android:layout_height="280dp"
        android:layout_marginTop="10dp"
        android:background="@color/white" />

    <TextView
        android:id="@+id/status_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Status"
        android:textColor="@color/white"
        android:textSize="20dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <ImageButton
            android:id="@+id/play_pause_btn"
            android:layout_width="70dp"

            android:layout_height="70dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/round_button"
            android:gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/play_btn" />

        <ImageButton
            android:id="@+id/sleep_timer_btn"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/play_pause_btn"
            android:background="@drawable/round_button"
            android:gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/sleep_btn" />

        <ImageButton
            android:id="@+id/share_btn"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginRight="30dp"
            android:layout_toLeftOf="@+id/play_pause_btn"
            android:background="@drawable/round_button"
            android:gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/share_btn" />

        <TextView
            android:id="@+id/sleep_timer_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sleep_timer_btn"
            android:layout_alignLeft="@+id/sleep_timer_btn"
            android:layout_alignRight="@+id/sleep_timer_btn"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="Status"
            android:textColor="@color/white"
            android:textSize="15dp" />

    </RelativeLayout>

    <SeekBar
        android:id="@+id/volume_seek_bar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:theme="@style/Volume_Seekbar"
        android:paddingStart="30dp"
        android:paddingEnd="30dp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

它在大多数设备中都能完美运行,但在小屏幕设备中我看不到SeekBar它,因为它不在屏幕上。知道我该如何解决吗?

Tam*_*bul 7

您在视图上使用了非常大的固定尺寸,并且由于不同手机的屏幕尺寸不同,因此在某些设备上似乎可以使用的内容实际上在其他设备上无法使用

例如- 在一个较小的设备中300dp,视图的总和为总400dp视图,由于屏幕尺寸小,您将没有足够的空间容纳某些视图,这就是您看不到视图的原因。


您可以使用ConstraintLayout 使您的屏幕响应,或者如果您想保持当前的布局结构,您可以使用以下库来缩放 dp 的大小:

sspsdp为您的视图和文本获取可扩展的大小单位。


另一种选择是用 包裹您的布局ScrollView,为您的屏幕提供可滚动选项,这样您就可以“看到”屏幕上的所有视图 - 请注意,这对您的用户来说可能不直观。