Android對於AutoLayout的優化

2022-09-23 12:27:08 字數 3270 閱讀 1980

最近在使用鴻洋大神寫的autolayout框架時,發現只支援linearlayout,relativelayout,framelayout這三種常用布局。但是我最近剛好準備使用drawerlayout發現無法適配。這讓我很尷尬阿 。偶然翻看原始碼,發現此事另有轉機阿。

下面我們來看一下原始碼:

public class autolayoutactivity extends appcompatactivity

if (name.equals(layout_linearlayout))

if (name.equals(layout_relativelayout))

if (view != null) return view;

return super.oncreateview(name, context, attrs);

}}首先這個是我們需要繼承的autolayoutactivity.class的原始碼很簡單。只需要根據型別返回乙個自定義auto的布局即可,我翻看了這幾個auto的布局,大致**基本一樣這裡只選取其中乙個類的原始碼

public class autorelativelayout extends relativelayout

public autorelativelayout(context context, attributeset attrs)

public autorelativelayout(context context, attributeset attrs, int defstyle)

@targetapi(build.version_codes.lollipop)

public autorelativelayout(context context, attributeset attrs, int defstyleattr, int defstyleres)

@override

public layoutparams generatelayoutparams(attributeset attrs)

@override

protected void onmeasure(int widthmeasurespec, int heightmeasurespec)

@override

protected void onlayout(boolean changed, int left, int top, int right, int bottom)

public static class layoutparams extends relativelayout.layoutparams

implements autolayouthelper.autolayoutparams

public layoutparams(int width, int height)

public layoutparams(viewgroup.layoutparams source)

public layoutparams(marginlayoutparams source)

@override

public autolayoutinfo getautolayoutinfo()

}}我發現這三個類的乙個共同點就是都繼承了原本的layout類,其他邏輯處理基本一樣,只有這個型別是不同的。這時我有乙個大膽的想法,如果我們在定義個相同模式的布局檔案會出現咋樣的情況呢?

由於我需要的是drawerlayout所以在此處定義乙個autodrawerlayout的類

public class autodrawerlayout extends drawerlayout

public autodrawerlayout(context context, attributeset attrs)

public autodrawerlayout(context context, attributeset attrs, int defstyleattr)

@override

public layoutparams generatelayoutparams(attributeset attrs)

@override

protected void onmeasure(int widthmeasurespec, int heightmeasurespec)

super.onmeasure(widthmeasurespec, heightmeasurespec);

}@override

protected void onlayout(boolean changed, int left, int top, int right, int bottom)

public static class layoutparams extends drawerlayout.layoutparams

implements autolayouthelper.autolayoutparams

public layoutparams(int width, int height)

public layoutparams(int width, int height, int gr**ity)

public layoutparams(viewgroup.layoutparams source)

public layoutparams(marginlayoutparams source)

public layoutparams(drawerlayout.layoutparams source)

public layoutparams(layoutparams source)

@override

public autolayoutinfo getautolayoutinfo()

}}並在autolayoutactivity中型別判斷的時候加入我的此種型別,修改後**如下

public class autolayoutactivity extends appcompatactivity

if (name.equals(layout_linearlayout))

if (name.equals(layout_relativelayout))

//drawerlayout相關

if (name.equals(layout_drawerlayout))

if (view != null) return view;

return super.oncreateview(name, context, attrs);

}}這樣autodrawerlayout就定義完成。

我認為其他的layout,比如constraintlayout這種也可以通過此種方式來進行px自適應。

如有不足歡迎評論補充。

android圓形旋轉選單,而對於移動轉換功能支援

lz該公司最近接手乙個專案,需要寫乙個圓形旋轉選單,和選單之間的移動換位支援,我本來以為這樣的demo如若網際網路是非常。想想你媽媽也幫不了我,空旋轉,但它不能改變位置,所以lz我們只能靠自己摸索。在此貢獻出來給廣大碼農們共享。話不多說,先上 自己定義view類 public class round...

Android4 4對於sdcard許可權的改動

我們在公升級4.4的過程中,發現了部分第三方應用不能使用。檢視發現以下幾點要求 1 api 19以上現在強制要求android.permission.read external storage 和android.permission.write external storage 許可權了。2 如果發...

Android對於外接TF卡的讀寫操作(許可權獲取)

android4.4 api級別19 引入了儲存訪問框架 saf saf其中的部分功能就是通過其獲取對外置 卡的讀寫許可權,從而操作外接 卡 原文介紹連線,這裡不對其進行複製貼上,本帖直接講述如何通過documentfile在獲得saf的後,在外置sd卡中讀寫資料 android 4.4提供了複雜的...