线性布局在小设备上不响应

huangapple go评论128阅读模式
英文:

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="标题"
            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="状态"
            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="状态"
                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`,因为它超出了屏幕。你有什么办法可以解决吗?
英文:

I have a layout file in my Android application:

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

It's working perfectly in most of the devices but in a small screen device I can't see the SeekBar because it's out from the screen. Any idea how I can fix it?

答案1

得分: 7

你在视图中使用了非常大的固定尺寸,因为不同的手机具有不同的屏幕尺寸,在某些设备上有效的尺寸实际上在另一个设备上可能无效。

例如 - 在高度为300dp的较小设备上,如果视图总和达到400dp,由于屏幕较小,您可能没有足够的空间容纳某些视图,这就是为什么您无法看到您的视图。


您可以使用ConstraintLayout使您的屏幕具有响应性,或者如果您希望保留当前的布局结构,则可以使用以下库来缩放dp的大小:

sspsdp,以获得可伸缩的视图和文本大小单位。


另一种选择是使用ScrollView包装您的布局,使您的屏幕具有可滚动性,从而您将能够在屏幕上“看到”所有视图 - 请注意,这可能对您的用户来说不直观。

英文:

You are using very large fixed sizes on your views, and because different phones got different screen sizes what seems to work on some device will actually not work on another

For example - in a smaller device with height of 300dp with views summing up to total of 400dp you will not have enough room for some views because of the small screen size and that's why you are not seeing your view.


You can use ConstraintLayout to make your screen responsive, or if you want to keep your current layout structure you can use the following libraries for scaling size of dp:

ssp and sdp to get a scalable size unit for your views and texts.


Another option is to wrap your layout with ScrollView, giving your screen the option to be scrollable and by that you will be able to "see" all of your views on your screen - note that this may not be intuitive to your user.

答案2

得分: 1

因为您使用的固定尺寸来构建布局,最终会累加到对于小屏幕尺寸来说过大。您可以尝试以下方法来解决这个问题:

  1. 通过使用最小宽度限定符为小屏幕尺寸创建一个专门的布局。
  2. 使您的布局可滚动。
  3. 使用约束布局。
  4. 使用sspsdp尺寸单位。

注意,您需要向当前布局指定最小宽度限定符,以使最小宽度限定符排除小屏幕设备,并包括适合您布局的设备。

英文:

It is because the fixed sizes you are using to build your layout is getting summed up eventually being too big for small screen size. You can try the following to fix this:

  1. Create a layout specially for small screen size by using smallest width qualifier
  2. Make your layout scrollable
  3. Use constraint layout
  4. Using the ssp and sdp size units

NOTE that you will have to specify the smallest width qualifier to your current layout in such a way that the smallest width qualifer excludes the small screen devices and include the devices in which your layout fits properly.

答案3

得分: 1

我建议您使用ConstraintLayout,但如果您仍然希望继续使用当前的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" />

        <!-- 其他视图元素... -->

        <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>
英文:

I would suggest you to use constraint layout, but if you still want to go ahead with your current view then it will be good practice if you add scrollview to your parent layout, so that your layout won't get cropped on smaller devices:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ScrollView
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fillViewport=&quot;true&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:background=&quot;@color/player_background&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.Fragments.PlayerFragment&quot;&gt;
&lt;ProgressBar
android:id=&quot;@+id/progress_bar_player&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;5dp&quot;
android:theme=&quot;@style/ProgressBarStyle&quot;
android:visibility=&quot;invisible&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/title_label&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Title&quot;
android:textColor=&quot;@color/white&quot;
android:textSize=&quot;24dp&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/view_imageview&quot;
android:layout_width=&quot;280dp&quot;
android:layout_height=&quot;280dp&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;@color/white&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/status_label&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Status&quot;
android:textColor=&quot;@color/white&quot;
android:textSize=&quot;20dp&quot; /&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;&gt;
&lt;ImageButton
android:id=&quot;@+id/play_pause_btn&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:background=&quot;@drawable/round_button&quot;
android:gravity=&quot;center_vertical|center_horizontal&quot;
android:padding=&quot;20dp&quot;
android:scaleType=&quot;fitCenter&quot;
android:src=&quot;@drawable/play_btn&quot; /&gt;
&lt;ImageButton
android:id=&quot;@+id/sleep_timer_btn&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:layout_toRightOf=&quot;@+id/play_pause_btn&quot;
android:background=&quot;@drawable/round_button&quot;
android:gravity=&quot;center_vertical|center_horizontal&quot;
android:padding=&quot;20dp&quot;
android:scaleType=&quot;fitCenter&quot;
android:src=&quot;@drawable/sleep_btn&quot; /&gt;
&lt;ImageButton
android:id=&quot;@+id/share_btn&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:layout_marginRight=&quot;30dp&quot;
android:layout_toLeftOf=&quot;@+id/play_pause_btn&quot;
android:background=&quot;@drawable/round_button&quot;
android:gravity=&quot;center_vertical|center_horizontal&quot;
android:padding=&quot;20dp&quot;
android:scaleType=&quot;fitCenter&quot;
android:src=&quot;@drawable/share_btn&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/sleep_timer_label&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/sleep_timer_btn&quot;
android:layout_alignLeft=&quot;@+id/sleep_timer_btn&quot;
android:layout_alignRight=&quot;@+id/sleep_timer_btn&quot;
android:layout_marginTop=&quot;5dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Status&quot;
android:textColor=&quot;@color/white&quot;
android:textSize=&quot;15dp&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;SeekBar
android:id=&quot;@+id/volume_seek_bar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;40dp&quot;
android:paddingStart=&quot;30dp&quot;
android:paddingEnd=&quot;30dp&quot;
android:theme=&quot;@style/Volume_Seekbar&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;

huangapple
  • 本文由 发表于 2020年7月27日 17:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63111984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定