簡體   English   中英

LinearLayout 在小型設備中沒有響應

[英]LinearLayout not responsive in small devices

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

它在大多數設備中都能完美運行,但在小屏幕設備中,我看不到SeekBar ,因為它不在屏幕上。 知道如何解決嗎?

您在視圖上使用了非常大的固定尺寸,並且由於不同的手機具有不同的屏幕尺寸,因此在某些設備上似乎可以正常工作,但實際上在其他設備上卻無法正常工作

例如- 在高度為300dp且視圖總計為400dp的較小設備中,由於屏幕尺寸小,您將沒有足夠的空間容納某些視圖,這就是您看不到視圖的原因。


您可以使用ConstraintLayou t 使屏幕響應,或者如果您想保持當前的布局結構,您可以使用以下庫來縮放 dp 的大小:

sspsdp為您的視圖和文本獲取可縮放的大小單位。


另一種選擇是用ScrollView包裹你的布局,讓你的屏幕可以滾動,這樣你就可以“看到”你屏幕上的所有視圖——注意這對你的用戶來說可能不直觀。

我建議您使用constraint layout ,但如果您仍想在當前view前使用scrollview ,那么最好將滾動視圖添加到父布局中,這樣您的布局就不會在較小的設備上被裁剪:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:paddingStart="30dp"
            android:paddingEnd="30dp"
            android:theme="@style/Volume_Seekbar" />

    </LinearLayout>
</ScrollView>

這是因為您用於構建布局的固定尺寸最終被總結為對於小屏幕尺寸來說太大了。 您可以嘗試以下方法來解決此問題:

  1. 使用最小寬度 限定符創建專門針對小屏幕尺寸的布局
  2. 使您的布局可滾動
  3. 使用約束布局
  4. 使用sspsdp大小單位

注意,您必須為當前布局指定最小寬度限定符,以使最小寬度限定符不包括小屏幕設備並包括適合您的布局的設備。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM