【问题标题】:How to define two different layouts for the layouts in the same category but slightly different inches?如何为同一类别但英寸略有不同的布局定义两种不同的布局?
【发布时间】:2013-12-29 12:24:34
【问题描述】:

我正在 Eclipse 的图形布局中测试我的布局。在图形布局中可以查看 5.4 英寸 FWVGA(480x854,mdpi) 和 5.1 英寸 WVGA(480x800,mdpi) 屏幕。如果我是正确的,它们都属于大屏幕类别,因此我已将布局 xml 放入 layout-large 文件夹。我已将图像放入 drawable-large-mdpi 文件夹。但是如下图所示,5.1 英寸 WVGA 中的图像按钮略微偏离。第一张是 5.1 英寸,第二张截图是 5.4 英寸,而且是正确的。

我的第二个问题与上述类似。我已经为 Nexus 7 完成了新布局,并将其放入 layout-sw600dp 文件夹,并将图像放入 drawable-tvdpi。然后我在 7 英寸平板电脑(1024x600,mdpi)中查看了这个布局。并且图像按钮比它应该看起来的稍微偏离。和第一个问题一样。

我知道原因可能是因为 5.1in 和 5.4in 并不完全相同,即使它们属于同一类别,我应该为它们设置 2 种不同的布局。那我应该怎么做呢?我怎样才能有两种不同的布局?还是有更好的解决方案,因为我不想为市场上存在的每种屏幕尺寸创建布局。 这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/wall4_cabinetclosed" android:orientation="vertical">

    <RelativeLayout android:id="@+id/frames_layout"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginLeft="73dp" android:layout_marginTop="57dp">

        <ImageView android:id="@+id/fire_frame"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
            android:background="@drawable/small_fire_icon" />

        <ImageView android:id="@+id/water_frame"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignTop="@+id/fire_frame" android:layout_marginLeft="75dp"
            android:layout_toRightOf="@+id/fire_frame" android:background="@drawable/small_water_icon" />

        <ImageView android:id="@+id/earth_frame"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_below="@+id/fire_frame" android:layout_marginTop="71dp"
            android:layout_toLeftOf="@+id/water_frame" android:background="@drawable/small_earth_icon" />

        <ImageView android:id="@+id/wind_frame"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/water_frame" android:layout_alignTop="@+id/earth_frame"
            android:background ="@drawable/small_wind_icon" />
    </RelativeLayout>

        <ImageView android:id="@+id/zoomed_image"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />

    <Button android:id="@+id/frames" android:layout_width="210dp"
        android:layout_height="220dp" android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" android:layout_marginLeft="55dp"
        android:layout_marginTop="30dp" android:onClick="zoomFrames" 
        android:background="@android:color/transparent"/>

    <Button android:id="@+id/blue" android:layout_width="115dp"
        android:layout_height="70dp" android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" android:layout_marginLeft="220dp"
        android:onClick="zoomImage" 
        android:background="@android:color/transparent"/>

    <Button android:id="@+id/white" android:layout_width="85dp"
        android:layout_height="50dp" android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" android:layout_marginRight="420dp"
        android:layout_marginTop="420dp" android:onClick="zoomImage"
        android:visibility="invisible"
        android:background="@android:color/transparent"/> 
        <ImageView
        android:id="@+id/fireplace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="311dp"
        android:layout_marginLeft="67dp"
        android:background="@drawable/fire"
     />
            <ImageButton
        android:id="@+id/water_room"
          android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="310dp"
        android:layout_marginLeft="90dp"
        android:background="@drawable/view_water_room"
        android:onClick="finishFireRoom"
                android:visibility="gone"
        />
        <ImageView android:id="@+id/frames_zoomed_image"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:background="@drawable/frames_zoomed" android:onClick="hideZoomedFrames"
        android:visibility="gone"/>

    <RelativeLayout android:id="@+id/frameZoomedLayout"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginTop="113dp" android:layout_marginLeft="145dp"
        android:visibility="gone"
        >

        <ImageButton android:id="@+id/fire_icon"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/fire_icon" android:onClick="changeIcon" />
        <ImageButton android:id="@+id/water_icon"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/fire_icon" android:layout_toRightOf="@+id/fire_icon"
            android:background="@drawable/water_icon" android:layout_marginLeft="147dp" android:onClick="changeIcon"/>
        <ImageButton android:id="@+id/earth_icon"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_below="@+id/fire_icon" android:layout_marginTop="140dp"
            android:layout_toLeftOf="@+id/water_icon" android:background="@drawable/earth_icon" android:onClick="changeIcon"/>

        <ImageButton android:id="@+id/wind_icon"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/water_icon" android:layout_alignTop="@+id/earth_icon"
            android:background="@drawable/wind_icon" android:onClick="changeIcon"/>
    </RelativeLayout>



</RelativeLayout>

【问题讨论】:

    标签: android layout android-screen-support


    【解决方案1】:

    在第三步中设置所有您的设备特定的,如屏幕、语言等。你完成了

    【讨论】:

    • 谢谢,我不知道你能做到这一点。但是,如果我要为每个屏幕尺寸都这样做,它需要做很多工作。如您所知,有几十种屏幕类型。我需要有这么多的布局副本吗?在我的布局文件夹中,我有 27 个 xml。如果我为每个屏幕都这样做,我将拥有 100 多个 xml。你认为还有其他方法吗?
    • 并非每个屏幕尺寸都需要。 android ui 开发不会那样进行。首先在每个屏幕上进行测试,如果您发现有问题,那就去做吧。否则只需为一种类型的屏幕集设置一些标准的 ui 设计。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多