安卓VS鴻蒙第三方件切換寶典 V2.0(第一部分)
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
眾所周知,安卓應(yīng)用開發(fā)經(jīng)過這么多年的發(fā)展相對成熟和穩(wěn)定,鴻蒙OS作為后來者兼容一個成熟的開發(fā)體系會節(jié)省很多推廣和開發(fā)成本。但在實際開發(fā)中,代碼層面仍然有很多細節(jié)上的差異,會給初次開發(fā)人員造成困擾。
本寶典旨在匯總實際開發(fā)中第三方件接入時的代碼差異,以期幫助開發(fā)人員更好的進行開發(fā)作業(yè),由于目前接觸的開發(fā)類型有限,所匯總的內(nèi)容多少會有疏漏,后期我們會進一步完善和補全。
歡迎關(guān)注我們以及我們的專欄,方便您及時獲得相關(guān)內(nèi)容的更新。
消息&多線程
1.設(shè)置線程級別
安卓:
- android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_XXXXX);
鴻蒙:
- ProcessManager.setThreadPriority(Thread.xxx);
2.判斷是否是主線程
安卓:
- Looper.getMainLooper().getThread() == Thread.currentThread()
鴻蒙:
- EventRunner.getMainEventRunner().isCurrentRunnerThread();
3.取消任務(wù)操作
安卓:
- task.cancel(true);
鴻蒙:
- task.revoke();
布局&組件
1.展示一段時間的提示信息彈框
安卓:
- Toast makeText(Context context,String msg,int duration).show();
鴻蒙:
- new ToastDialog(Context context).setText(String msg).show();
2.給控件設(shè)置它的左中右上的圖片
安卓:
1.代碼中:
- Drawable dwLeft = getResources().getDrawable(R.mipmap.ic_launcher);
- dwLeft.setBounds(0, 0, dwLeft.getMinimumWidth(), dwLeft.getMinimumHeight());
- View.setCompoundDrawables(dwLeft, null, null, null);
2.布局中:
- android:drawableLeft=“@mipmap/ic_launcher”
- android:drawableTop=“@mipmap/ic_launcher”
- android:drawableRight=“@mipmap/ic_launcher”
- android:drawableBottom=“@mipmap/ic_launcher”
鴻蒙:
1.代碼中:
- Resource resource = context.getResourceManager().getResource(ResourceTable.Media_select);
- PixelMapElement element = new PixelMapElement(resource);
- companent.setAroundElements(element,null,null,null);
2.布局中:
- ohos:element_left=“media:icon" ohos:element_right="media:icon"ohos:element
- r
-
- ight="media:icon”
- ohos:element_top=“media:icon" ohos:element_bottom="media:icon"ohos:element
- b
-
- ottom="media:icon”
3.設(shè)置控件的多態(tài)化
安卓:
1.在布局中給控件設(shè)置:android:background=“@drawable/bt_login”
2.在xml中實現(xiàn)bt_login
- <?xml version=“1.0” encoding=“utf-8”?>
- <selector xmlns:android=“http://schemas.android.com/apk/res/android”>
- <!-- 控件點擊狀態(tài) -->
- <item android:drawable=“@drawable/button_down” android:state_pressed=“true”></item>
- <!-- 控件選中狀態(tài) -->
- <item android:drawable=“@drawable/button_down” android:state_checked=“true”></item>
- </selector>
鴻蒙:
- Resource selectResource = context.getResourceManager().getResource(ResourceTable.Media_select);
- Resource emptyResource = context.getResourceManager().getResource(ResourceTable.Media_unselect);
- PixelMapElement selectElement = new PixelMapElement(selectResource);
- PixelMapElement emptyElement = new PixelMapElement(emptyResource);
- StateElement checkElement = new StateElement();
- checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_SELECTED}, selectElement);
- checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, emptyElement);
- component.setButtonElement(null);
- component.setAroundElements(checkElement,null,null,null);
- component.setSelected(true);
4.scrollView滾動
安卓:
- <ScrollView
- android:layout_width=“match_parent”
- android:layout_height=“match_parent”>
- <LinearLayout
- android:layout_width=“match_parent”
- android:layout_height=“match_parent”
- android:orientation=“vertical”>
- </LinearLayout>
- </ScrollView>
鴻蒙:
- <ScrollView
- ohos:height=“match_content”
- ohos:width=“match_parent”
- >
- <DirectionalLayout
- ohos:height=“match_content”
- ohos:width=“match_content”
- ohos:orientation=“vertical”>
- </DirectionalLayout>
- </ScrollView>
5.組件隱藏
安卓:
- 不可見: android:visibility=“invisible”; Java代碼:view.setVisibility(View.INVISIBLE); 隱藏: android:visibility=“gone”; Java代碼:view.setVisibility(View.GONE);
鴻蒙:
- ohos:visibility=“hide”;comp.setVisibility(HIDE);ohos:visibility=“invisible”;comp.setVisibility(INVISIBLE)
6.線性布局
安卓:
- LinearLayout
鴻蒙:
- DirectionalLayout
7.相對布局
安卓:
- RelativeLayout
鴻蒙:
- DependentLayout
8.幀布局
安卓:
- FrameLayout
鴻蒙:
- StackLayout
9.選項卡
安卓:
- TabLayout
鴻蒙:
- TabList和Tab
10.像素單位
安卓:
- dp
鴻蒙:
- vp
11.控件的對齊方式
安卓:
- Gravity.CENTER
鴻蒙:
- LayoutAlignment.CENTER
12.布局名稱及用法
安卓:
- RelativeLayout相對布局LinearLayout線性布局
鴻蒙:
- DependentLayout相對布局 DirectionalLayout線性布局
13.自定義布局
安卓:
- View inflate =
- inflate(getContext(), R.layout.reg_view, this);
鴻蒙:
- component = LayoutScatter.getInstance(context).parse(com.istone.videocache.ResourceTable.Layout_ability_player,layout,false);
- layout.addComponent(component, 0);
14.疊加布局,層級疊加
安卓:
- FrameLayout
鴻蒙:
- StackLayout
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)