【问题标题】:How to deal with Android XLarge screen?安卓超大屏怎么处理?
【发布时间】:2012-07-20 17:20:28
【问题描述】:

我开发了一个时钟小部件。在许多设备上看起来都不错。摩托罗拉 Xoom 除外,因为它属于 XLARGE 类别。

这是clock.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00000000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<AnalogClock android:id="@+id/AnalogClock" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:dial="@drawable/widgetclock" 
    android:hand_hour="@drawable/widgethour" 
    android:hand_minute="@drawable/widgetminute" /> 

    <!-- Time Row -->
    <LinearLayout
        android:id="@+id/lltimeheaderrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:orientation="horizontal"
        android:layout_gravity="center|top"
        android:layout_marginTop="45dp"
        android:visibility="gone">

        <!-- TIME -->
        <LinearLayout android:id="@+id/llTimeDefault"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center">

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

            <TextView
                android:id="@+id/HOUR"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/my_gray"
                android:textStyle="bold"
                android:textSize="15sp"/>

            <TextView
                android:id="@+id/MINUTE"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/my_gray"
                android:textStyle="bold"
                android:textSize="15sp" />
            </LinearLayout>

            <TextView
                android:id="@+id/AM_PM"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/my_gray"
                android:textStyle="bold"
                android:textSize="13sp"
                android:padding="1dp" />                        
        </LinearLayout> 
    </LinearLayout>     

    <!-- Calendar Graph -->
    <LinearLayout
        android:id="@+id/llCalGraph"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center|left"    
        android:layout_marginLeft="45dp">   
            <FrameLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <ImageView
                    android:id="@+id/CalendarImg"
                    android:src="@drawable/calendar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>                      
                <TextView android:id="@+id/DateDayG"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center|top"
                    android:layout_marginTop="3dp"
                    android:textSize="11sp"
                    android:textStyle="bold"
                    android:textColor="@color/my_gray"
                    android:shadowColor="@color/my_black"
                    android:shadowDx="1"
                    android:shadowDy="1"
                    android:shadowRadius="2"                
                    ></TextView>       
                <TextView android:id="@+id/DateNoG"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:textSize="25sp"
                    android:textColor="@color/my_gray"
                    ></TextView>    
            </FrameLayout>              

    </LinearLayout>             

    <!-- BATTERY -->
    <LinearLayout
        android:id="@+id/llBattGraph"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center|right"
        android:layout_marginRight="45dp">              
            <FrameLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <ImageView
                    android:id="@+id/BatteryImg"
                    android:src="@drawable/batt_00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:id="@+id/BatteryFlashing"
                    android:src="@drawable/flashing"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="13dp"
                    android:layout_marginLeft="2dp"
                    android:visibility="gone" />                        
                <TextView
                    android:id="@+id/BatteryLevel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/my_gray"
                    android:textSize="18sp"
                    android:layout_gravity="center" />


            </FrameLayout>              
    </LinearLayout>                     

    <!-- Weather Row -->
    <LinearLayout
        android:id="@+id/llweatherrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:orientation="horizontal"
        android:layout_gravity="center|bottom"
        android:layout_marginBottom="45dp"> 

        <!-- WEATHER IMAGE -->
        <LinearLayout
            android:id="@+id/llWeatherImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:layout_gravity="center">

            <ImageView android:id="@+id/ivWeather"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/w3200"
                />
        </LinearLayout> 

    </LinearLayout>     

</FrameLayout>

基本上它在小部件上有 4 个元素。上,下。左右部分。 我只是使用 FrameLayout 作为基本布局,每个元素都使用 LinearLayout,每个角落的重力为 45dp。

这是小部件在 Xoom 上的外观 我希望元素位置应该在我画的红色圆圈上。

  1. 我应该使用哪种最佳布局组合?你能推荐一下吗?
  2. 假设我坚持这种布局。如何为 Xoom 设备提供替代布局?我尝试将clock.xml 放在layout-xlarge 和drawable-xlarge 上,但没有...

【问题讨论】:

    标签: android layout xoom


    【解决方案1】:

    最好和更简洁的方法是创建一个文件夹 layout-xlarge,并在其中包含目标设备具有 xlarge 布局时将加载的 clock.xml

    这样你可以根据屏幕大小、密度等做出不同的布局。来自开发者文档http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

    另一种解决方案是将某个 45dpXdp 中的所有尺寸放入名为 dimen 的文件中的 values 文件夹中,然后在您的 xml 中将它们声明为:...="@dimen/largeButton" 并使用相同的如前所述,创建一个对应的values-xlarge,并在其中添加相应的dimen文件。

    这是非常有用的机制,因为您可以根据屏幕的大小创建不同的布局,并使其能够获得每个大小的不同密度像素。如果您还没有听说过这些条款中的任何一个,请让我提供更多详细信息。

    编辑:来自文档

    res/layout/my_layout.xml             // layout for normal screen size ("default")
    
    res/layout-small/my_layout.xml       // layout for small screen size
    
    res/layout-large/my_layout.xml       // layout for large screen size
    
    res/layout-xlarge/my_layout.xml      // layout for extra large screen size
    
    res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
    

    您确定是这种目录吗?与 clock.xml 这样的布局在任何地方都有相同的文件名?

    另一个编辑评论:

    它的效率要高得多,因为您可以猜到,仅替换数字而不是加载完整的布局文件夹的转换效率要高得多。尽管如此,您可以重用维度而不是在布局之间来回检查“那个维度又是什么?”就像在一个大按钮的固定尺寸中一样。

    更重要的是,每个文件夹都可以出于特定原因进行扩展。如果您想要相同的布局但不同的尺寸,那么您应该覆盖尺寸而不是复制粘贴整个布局。更有意义,不是吗?

    而努力几乎是一样的。就像layout-large/ 文件夹一样,您将拥有values-large/。在我提供的文档和互联网上都有一个很好的例子。

    【讨论】:

    • layout-xlarge 不起作用此文件夹中的 xml 永远不会被加载。为什么?
    • 对不起我的错误,它应该是 layout-large。顺便说一句,为什么 800x1280 和 149 dpi 是 LARGE 而不是 XLARGE。根据developer.android.com/guide/practices/… 1280x800(WXGA) 和 160dpi 被认为是 XLARGE
    • 我已经编辑了我的答案以供将来使用并涵盖所有细节。我不知道每部手机的规格,但在我提供的文档中,有关于您可以在市场上找到的每个屏幕宽度和屏幕密度的完整详细信息,以及您需要添加什么来定制它。虽然新的layout-xxx/ 文件夹是提供新布局的一种非常简单的方法,但请更详细地检查我提到的@dimen 解决方案,它效率更高,您不应该使用...=45dp。只是一个建议...... :)
    • 是的,我也要试试 dimen 解决方案。但这将是同样的努力,对吧? IE。根据每个屏幕尺寸提供 2 个或更多布局/值维度。以什么方式dimen解决方案会更有效?无论如何,你能告诉我更多关于 Xdp 的信息吗?我从来没有听说过。
    • 不,你弄错了。 Xdp 只是一个因素,就像 X 等于你想要的任何数字,只是为了提供一个例子。检查我更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2017-02-02
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多