我开发了一个时钟小部件。在许多设备上看起来都很好。摩托罗拉 Xoom 除外,因为它属于 XLARGE 类别。
这是时钟.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 上的外观 我期望元素位置应该在我绘制的红色圆圈上。
- 我应该使用什么最佳布局组合?你能建议一下吗?
- 假设我坚持这种布局。如何为 Xoom 设备提供替代布局?我尝试将clock.xml放在layout-xlarge和drawable-xlarge上,但什么也没有......
最佳答案
最好、更简洁的方法是创建一个文件夹 layout-xlarge
并在其中包含当目标设备具有 xlarge 布局时将加载的 clock.xml
.
这样你就可以根据屏幕尺寸、密度等做出不同的布局。来自开发者文档 http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
另一个解决方案是将特定 45dp
或 Xdp
中的所有尺寸放入名为 dimen
的文件中的 value 文件夹中,然后然后在 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/
。我提供的文档和互联网上有一个非常好的示例。
关于android - 如何应对Android XLarge屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584107/