Android中如何让控件居中


测试部报告锁屏界面里,设置锁屏的那个控件位置不好,有点偏下。以至于设置的时候,图标都在界外了。

打开packages/app/Settings里的res/layout-sw720dp/lockscreen_targets.xml

对于横屏和竖屏两种情况,其实应该定义两个目录一种叫做land,另一种叫做port(都是缩写),分别对应横屏和竖屏。

这里就增加一个目录layout-sw720dp-land/作为横屏的配置, layout-sw720dp为竖屏。

对于竖屏的情况

--- a/res/layout-sw720dp/lockscreen_targets.xml
+++ b/res/layout-sw720dp/lockscreen_targets.xml
@@ -26,7 +26,7 @@
         Android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="0.9"
-        android:gravity="left|top"
+        android:gravity="top"
         prvandroid:targetDrawables="@*android:array/lockscreen_targets_with_camera"
         prvandroid:targetDescriptions="@*android:array/lockscreen_target_descriptions_with_camera"
         prvandroid:directionDescriptions="@*android:array/lockscreen_direction_descriptions"


对于横屏

将+        android:gravity="top"改为+        android:gravity="left".

就可以居中显示了。

更保险的方法是

横屏: android:gravity="left|center_vertical"

竖屏:android:gravity="top|center_horizontal"

相关内容