【问题标题】:How to make my layout fit in all screens如何使我的布局适合所有屏幕
【发布时间】:2014-02-25 05:47:02
【问题描述】:

我在 stockoverflow 和链接上浏览了许多相关答案,例如 Supporting Different ScreensScreen Compatibility Mode
我创建了以下相对布局,并试图使其在所有 android 屏幕中看起来都一样。

我的图像非常适合 4.8 英寸手机,但是当我尝试使用 2.8 英寸显示屏或类似设备时,一些按钮会在其他按钮之上,而且它们不会缩小。
有没有人对如何改进有任何建议?
最好不要增加我的应用程序的大小。

提前致谢!

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

【问题讨论】:

  • 在布局中添加一个 ScrollView。
  • 能否展示两张图片:一张使用 4.8" 屏幕,一张使用 2.8" 屏幕?
  • “我已经创建了以下相对布局,并且我试图让它在所有 android 屏幕中看起来都一样”——这不应该是你的目标。您的目标应该是为所有 Android 屏幕尺寸提供有用的 UI。这可能需要同一布局资源的多个版本,尤其是处理-small 2.8" 等屏幕。the first page that you linked to 涵盖了这一点。
  • ScrollView 将防止视图在您的布局中重叠。然而,支持多种屏幕尺寸以及让图像看起来不错的问题是更大的问题,它在您提供的链接中有所涵盖。坚持下去。
  • 好的,非常感谢!

标签: android android-layout layout android-relativelayout imagebutton


【解决方案1】:

我会建议您使用 LinearLayout 而不是专门针对该问题的 Relativelayout :

我的图像非常适合 4.8 英寸手机,但当我尝试使用 2.8 英寸显示器或类似的东西,一些按钮位于顶部 其他的,它们不会缩小。

因为当您将 orientation 赋予 Linearlayout 时,它的子级将永远不会在任何分辨率或屏幕尺寸下相互重叠。

如果你给它们权重,那么每个设备的视图都将处于固定位置。

例如,在您的项目中放在 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>

【讨论】:

  • 如果您有任何疑问,可以分享您的疑问。
  • 无法让它在我的代码中工作:/,即使我添加了weightlinearlayout
  • 你可以发送你在上面的xml中使用过的所有图像吗?所以我可以测试一下。
猜你喜欢
  • 1970-01-01
  • 2021-06-21
  • 2015-02-06
  • 1970-01-01
  • 2017-02-16
  • 2021-09-15
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多