简体   繁体   English

如何使我的布局适合所有屏幕

[英]How to make my layout fit in all screens

I have gone through many relative answers on stockoverflow and link's such as Supporting Different Screens or Screen Compatibility Mode 我在库存溢出和链接方面经历了许多相对答案,例如支持不同的屏幕屏幕兼容模式
I have created the following relative layout and I am trying to make it look alike in all android screens. 我创建了以下相对布局,并试图使它在所有android屏幕中看起来都一样。

My images fit perfect for 4.8 inch phones but when I try to use a 2.8 inch display or something like like that, some buttons go on top of others, and they do not shrink. 我的图像非常适合4.8英寸手机,但是当我尝试使用2.8英寸显示器或类似的东西时,某些按钮会叠在其他按钮上,并且不会缩水。
Does anyone have any suggestion how to improve that? 有没有人建议如何改进它?
And preferably without increasing the size of my app. 并且最好不要增加我的应用程序的大小。

Thanks in advance! 提前致谢!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:mm="http://millennialmedia.com/android/schema"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/Switch_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="someMethod"
        android:background="@drawable/switch_off"/>

    <ImageView
        android:id="@+id/warning_shot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/more"
        android:onClick="someMethod" />

    <ImageView
        android:id="@+id/imageViewBatteryState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"        
        android:layout_below="@+id/Switch_off"

        />

   <ImageView
        android:id="@+id/sh"
        android:layout_width="147dp"
        android:layout_height="54dp"
        android:layout_below="@+id/Switch_off"
        android:layout_centerHorizontal="true"
        android:background="@drawable/me"/>

    <ImageView
        android:id="@+id/sit"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_below="@+id/Switch_off"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/textViewBatteryInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="12sp"
        android:textStyle="bold"
        android:typeface="monospace" 
        android:layout_alignBottom="@+id/sit"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

I will suggest you to use LinearLayout instead of Relativelayout specially for the issue : 我建议您专门针对此问题使用LinearLayout而不是Relativelayout:

My images fit perfect for 4.8 inch phones but when I try to use a 2.8 inch display or something like like that, some buttons go on top of others, and they do not shrink. 我的图像非常适合4.8英寸手机,但是当我尝试使用2.8英寸显示器或类似的东西时,某些按钮会叠在其他按钮上,并且不会缩水。

Because When you give orientation to Linearlayout then Its child will never get on top of each other for any resolution or screen size. 因为当您为Linearlayout设置方向时 ,其子级将永远不会在任何分辨率或屏幕尺寸上相互重叠。

And If you give them Weight then Views will be in fix position for Each and Every device. 而且,如果您赋予它们权重,则每个设备的视图都将处于固定位置。

For example Put below Xml in your project and Check this out in Any resolution, It will give the same Result. 例如,将Xml下面放在您的项目中,然后以“任何分辨率”将其出,它将得到相同的结果。

XML: XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:mm="http://millennialmedia.com/android/schema"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 3" />
    </LinearLayout>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM