转载参考下: http://blog.csdn.net/xch520xx/article/details/51285927
最近在公司在处理Launcher桌面,横屏的时候要求桌面的 Hotseat在底部(竖屏的时候Hotseat刚好在底部),而我们刚开始的时候Hotseat却在右侧。之前也处理过相应的问题,一般来说,这个情况可以通过修改分辨率可以解决,可以试着修改build.prop这个属性文件,并推送到系统里面。修改值如下(参考值):
- </pre><pre name="code" class="plain">ro.sf.lcd_density=160
这个方法可能是系统自适应的结果,当屏幕分辨率调小后,图标都变小,此时横屏将Hotseat放在底部比较合适,相反,如果在屏幕分辨率比较大,hotseat在底部,在一定程度上回让屏幕变窄很不协调,比如之前我们的屏幕是 6*5的,如果放在底部,就变成了6*4,可以想象给人感觉都不好。
刚才是闲扯了,现在看看代码是怎么处理的。先看hotseat.xml文件这个应该是布局
- <com.android.launcher3.Hotseat
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:launcher="http://schemas.android.com/apk/res-auto">
- <com.android.launcher3.CellLayout
- android:id="@+id/layout"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_gravity="center" />
- </com.android.launcher3.Hotseat>
该布局的逻辑主要在Hotseat.Java中,我们继续进去看看
- public Hotseat(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- mLauncher = (Launcher) context;
- mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
- }
这是Hotseat的一个构造方法,一眼看去,就知道第4行是横竖屏的判断,在该类的下面有很多都是根据mHasVerticalHotseat的值来判断用相应的参数。既然竖屏的时候hotseat在底部,我们可以尝试将横屏时候的调用竖屏时的布局,这样就可以实现了。我们再去看看里面是怎么判断的。我们进去一看跳转到了 DeviceProfile.java类里面相关代码如下:
-
-
-
-
- boolean isVerticalBarLayout() {
- return isLandscape && transposeLayoutWithOrientation;
- }
看上面的注释,大概就知道,当返回值为true时 hotseat is on the bottom row,当为false时 hotseat is on the right column;这个很接近答案了。
接着看该类的代码:
- transposeLayoutWithOrientation =
- res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
- public DeviceProfile(Context context, InvariantDeviceProfile inv,
- Point minSize, Point maxSize,
- int width, int height, boolean isLandscape) {
-
- this.inv = inv;
- this.isLandscape = isLandscape;
- ...
- ...
- }
transposeLayoutWithOrientation是获取一个bool值,在属性配置文件中可以找到,默认为true;
我们看看这个 isLandscape 是在 DeviceProfile类的构造方法里面传入的。
我们再继续查找在哪里调用了这个构造方法,最后在InvariantDeviceProfile.java类中找到了:
- landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
- largeSide, smallSide, true );
- portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
- smallSide, largeSide, false );
以上差不多是修改hotseat的相关代码,可以用来借鉴参考下。如有什么问题,还请指教!