Android動態布局framelayout

功能說明
最近碰到一個需求,要求在網頁端拖控件,動態配置app控件的模塊,大小和位置,顯示不同的功能,然后在app大屏展示。
技術難點:
1.動態控件位置和大小難調,會出現布局混亂,位置錯亂等問題
2.動態控件樣式多樣,有相冊,有新聞列表,有個人信息展示,有輪播圖等等,解決方案就是用cardview嵌入frame layout,引入fragment,獨立布局

代碼說明:
step1:引入依賴,用gson進行json解析~\app\build.gradle
step2:清單文件,用于新增application,獲取全局context~\app\src\main\AndroidManifest.xml
step3:主界面布局~\app\src\main\res\layout\activity_main.xml
step4:json文件,模擬后臺數據,動態控制控件位置和大小~\app\src\main\assets\stu1.json
step5:同上~\app\src\main\assets\stu2.json
step6:同上~\app\src\main\assets\stu3.json
step7:全局context ~\app\src\main\java\com\example\iosdialogdemo\IvApplication.java
step8:數據bean類~\app\src\main\java\com\example\iosdialogdemo\CardBean.java
step9:動態控件設置,設置位置大小 ~\app\src\main\java\com\example\iosdialogdemo\AdaptScreenUtils.java
step10:主界面,json解析和判斷控件位置大小參數設置~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java
效果圖
在這里插入圖片描述

step1:~\app\build.gradle

    implementation 'com.google.code.gson:gson:2.8.0'

step2:~\app\src\main\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.iosdialogdemo"><applicationandroid:name=".IvApplication"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.IosDialogDemo"><activity android:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

step3:~\app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/darker_gray"android:orientation="vertical"android:padding="15dp"><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:visibility="gone"app:cardBackgroundColor="@android:color/holo_purple"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:visibility="gone"app:cardBackgroundColor="@android:color/holo_red_light"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_orange_light"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_green_dark"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_blue_dark"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_purple"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_red_light"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_orange_dark"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_purple"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_orange_light"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_10"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_green_dark"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView><androidx.cardview.widget.CardViewandroid:id="@+id/auto_card_11"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_margin="10dp"app:cardBackgroundColor="@android:color/holo_blue_dark"app:cardCornerRadius="10dp"app:contentPadding="20dp"></androidx.cardview.widget.CardView></FrameLayout>

step4:~\app\src\main\assets\stu1.json

[{"Id": 0,"studentName": "第一個card","location": 0,"size": 11,"isShow": true},{"Id": 1,"studentName": "第二個card","location": 1,"size": 21,"isShow": true},{"Id": 2,"studentName": "第三個card","location": 2,"size": 21,"isShow": false},{"Id": 3,"studentName": "第四個card","location": 3,"size": 11,"isShow": true},{"Id": 4,"studentName": "第五個card","location": 4,"size": 11,"isShow": true},{"Id": 5,"studentName": "第六個card","location": 5,"size": 12,"isShow": true},{"Id": 6,"studentName": "第七個card","location": 6,"size": 12,"isShow": true},{"Id": 7,"studentName": "第八個card","location": 7,"size": 11,"isShow": true},{"Id": 8,"studentName": "第九個card","location": 8,"size": 11,"isShow": true},{"Id": 9,"studentName": "第十個card","location": 9,"size": 11,"isShow": false},{"Id": 10,"studentName": "第十一個card","location": 10,"size": 11,"isShow": false},{"Id": 11,"studentName": "第十二個card","location": 11,"size": 11,"isShow": true}
]

step5:~\app\src\main\assets\stu2.json

[{"Id": 0,"studentName": "第一個card","location": 0,"size": 11,"isShow": true},{"Id": 1,"studentName": "第二個card","location": 1,"size": 21,"isShow": true},{"Id": 2,"studentName": "第三個card","location": 2,"size": 21,"isShow": true},{"Id": 3,"studentName": "第四個card","location": 3,"size": 11,"isShow": false},{"Id": 4,"studentName": "第五個card","location": 4,"size": 11,"isShow": true},{"Id": 5,"studentName": "第六個card","location": 5,"size": 11,"isShow": true},{"Id": 6,"studentName": "第七個card","location": 6,"size": 12,"isShow": true},{"Id": 7,"studentName": "第八個card","location": 7,"size": 12,"isShow": true},{"Id": 8,"studentName": "第九個card","location": 8,"size": 11,"isShow": true},{"Id": 9,"studentName": "第十個card","location": 9,"size": 11,"isShow": true},{"Id": 10,"studentName": "第十一個card","location": 10,"size": 11,"isShow": false},{"Id": 11,"studentName": "第十二個card","location": 11,"size": 11,"isShow": false}
]

step6:~\app\src\main\assets\stu3.json

[{"Id": 0,"studentName": "第一個card","location": 0,"size": 11,"isShow": true},{"Id": 1,"studentName": "第二個card","location": 1,"size": 12,"isShow": true},{"Id": 2,"studentName": "第三個card","location": 2,"size": 12,"isShow": true},{"Id": 3,"studentName": "第四個card","location": 3,"size": 11,"isShow": true},{"Id": 4,"studentName": "第五個card","location": 4,"size": 11,"isShow": true},{"Id": 5,"studentName": "第六個card","location": 5,"size": 12,"isShow": false},{"Id": 6,"studentName": "第七個card","location": 6,"size": 12,"isShow": false},{"Id": 7,"studentName": "第八個card","location": 7,"size": 11,"isShow": true},{"Id": 8,"studentName": "第九個card","location": 8,"size": 11,"isShow": true},{"Id": 9,"studentName": "第十個card","location": 9,"size": 21,"isShow": true},{"Id": 10,"studentName": "第十一個card","location": 10,"size": 11,"isShow": false},{"Id": 11,"studentName": "第十二個card","location": 11,"size": 11,"isShow": true}
]

step7:~\app\src\main\java\com\example\iosdialogdemo\IvApplication.java

package com.example.iosdialogdemo;import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;import androidx.annotation.RequiresApi;import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;public class IvApplication extends Application {public static Context context;private static IvApplication application;@Overridepublic void onCreate() {super.onCreate();context = this;application = this;}public static IvApplication getInstance() {return application;}public static Context getContext() {return context;}}

step8:~\app\src\main\java\com\example\iosdialogdemo\CardBean.java

package com.example.iosdialogdemo;public class CardBean {/*模塊大小 11  12  21   22 等等模塊位置     0 1 2 3 4 5 6 7 8默認所有按鈕全部隱藏  然后在代碼里設置顯示"Id": 0,"studentName": "第一個card","location": 0,"size": 11,"isShow": true*/private int id;private String studentName;private int location;private int size;private boolean isShow;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getLocation() {return location;}public void setLocation(int location) {this.location = location;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}public boolean isShow() {return isShow;}public void setShow(boolean show) {isShow = show;}
}

step9:~\app\src\main\java\com\example\iosdialogdemo\AdaptScreenUtils.java

package com.example.iosdialogdemo;import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;import androidx.annotation.NonNull;public final class AdaptScreenUtils {public static int getScreenWidth() {Resources resources = IvApplication.getContext().getResources();DisplayMetrics dm = resources.getDisplayMetrics();return dm.widthPixels;}public static int getScreenHeight() {Resources resources = IvApplication.getContext().getResources();DisplayMetrics dm = resources.getDisplayMetrics();return dm.heightPixels;}public static int getScreenItemWidth() {Resources resources = IvApplication.getContext().getResources();DisplayMetrics dm = resources.getDisplayMetrics();return dm.widthPixels / 4;}public static int getScreenItemHeight() {Resources resources = IvApplication.getContext().getResources();DisplayMetrics dm = resources.getDisplayMetrics();return dm.heightPixels / 3;}public static void getScreenHeight5(View view, int point, int widthSize, int heightSize, int widthLocation, int heightLocation) {switch (point) {case 0:FrameLayout.LayoutParams params0 = (FrameLayout.LayoutParams) view.getLayoutParams();params0.width = widthSize;params0.height = heightSize;params0.setMargins(0, 0, 0, 0);view.setLayoutParams(params0);break;case 1:FrameLayout.LayoutParams params1 = (FrameLayout.LayoutParams) view.getLayoutParams();params1.width = widthSize;params1.height = heightSize;params1.setMargins(widthLocation, 0, 0, 0);view.setLayoutParams(params1);break;case 2:FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) view.getLayoutParams();params2.width = widthSize;params2.height = heightSize;params2.setMargins(widthLocation * 2, 0, 0, 0);view.setLayoutParams(params2);break;case 3:FrameLayout.LayoutParams params3 = (FrameLayout.LayoutParams) view.getLayoutParams();params3.width = widthSize;params3.height = heightSize;params3.setMargins(widthLocation * 3, 0, 0, 0);view.setLayoutParams(params3);break;case 4:FrameLayout.LayoutParams params4 = (FrameLayout.LayoutParams) view.getLayoutParams();params4.width = widthSize;params4.height = heightSize;params4.setMargins(0, heightLocation, 0, 0);view.setLayoutParams(params4);break;case 5:FrameLayout.LayoutParams params5 = (FrameLayout.LayoutParams) view.getLayoutParams();params5.width = widthSize;params5.height = heightSize;params5.setMargins(widthLocation, heightLocation, 0, 0);view.setLayoutParams(params5);break;case 6:FrameLayout.LayoutParams params6 = (FrameLayout.LayoutParams) view.getLayoutParams();params6.width = widthSize;params6.height = heightSize;params6.setMargins(widthLocation * 2, heightLocation, 0, 0);view.setLayoutParams(params6);break;case 7:FrameLayout.LayoutParams params7 = (FrameLayout.LayoutParams) view.getLayoutParams();params7.width = widthSize;params7.height = heightSize;params7.setMargins(widthLocation * 3, heightLocation, 0, 0);view.setLayoutParams(params7);break;case 8:FrameLayout.LayoutParams params8 = (FrameLayout.LayoutParams) view.getLayoutParams();params8.width = widthSize;params8.height = heightSize;params8.setMargins(0, heightLocation * 2, 0, 0);view.setLayoutParams(params8);break;case 9:FrameLayout.LayoutParams params9 = (FrameLayout.LayoutParams) view.getLayoutParams();params9.width = widthSize;params9.height = heightSize;params9.setMargins(widthLocation, heightLocation * 2, 0, 0);view.setLayoutParams(params9);break;case 10:FrameLayout.LayoutParams params10 = (FrameLayout.LayoutParams) view.getLayoutParams();params10.width = widthSize;params10.height = heightSize;params10.setMargins(widthLocation * 2, heightLocation * 2, 0, 0);view.setLayoutParams(params10);break;case 11:FrameLayout.LayoutParams params11 = (FrameLayout.LayoutParams) view.getLayoutParams();params11.width = widthSize;params11.height = heightSize;params11.setMargins(widthLocation * 3, heightLocation * 2, 0, 0);view.setLayoutParams(params11);break;}}
}

step10:~\app\src\main\java\com\example\iosdialogdemo\MainActivity.java

package com.example.iosdialogdemo;import android.os.Bundle;
import android.util.Log;import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewTreeObserver;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;
import java.util.List;public class MainActivity extends Activity {private List<CardBean> stuList;private int width=0;private int height=0;private String jsonString;private CardView card_0, card_1, card_2, card_3,card_4, card_5, card_6, card_7,card_8, card_9, card_10, card_11;private int[] cardInts0 = {11, 21, 0, 11,11, 12, 12, 11,11, 0, 0, 11};private Handler mHandler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(@NonNull Message message) {if (message.what == 100) {jsonString = (String) message.obj;Log.i("INFO", "##### Sub_json: " + ": " + jsonString);Gson gson = new Gson();//解析Type type = new TypeToken<List<CardBean>>() {}.getType();if (stuList != null) {stuList.clear();}stuList.addAll(gson.fromJson(jsonString, type));Log.e("INFO", "##### Sub_list: " + ": " + stuList);//根據傳遞過來的值  來控制控件是否顯示if (stuList != null) {if (stuList.get(0).isShow()) {card_0.setVisibility(View.VISIBLE);} else {card_0.setVisibility(View.GONE);}if (stuList.get(1).isShow()) {card_1.setVisibility(View.VISIBLE);} else {card_1.setVisibility(View.GONE);}if (stuList.get(2).isShow()) {card_2.setVisibility(View.VISIBLE);} else {card_2.setVisibility(View.GONE);}if (stuList.get(3).isShow()) {card_3.setVisibility(View.VISIBLE);} else {card_3.setVisibility(View.GONE);}if (stuList.get(4).isShow()) {card_4.setVisibility(View.VISIBLE);} else {card_4.setVisibility(View.GONE);}if (stuList.get(5).isShow()) {card_5.setVisibility(View.VISIBLE);} else {card_5.setVisibility(View.GONE);}if (stuList.get(6).isShow()) {card_6.setVisibility(View.VISIBLE);} else {card_6.setVisibility(View.GONE);}if (stuList.get(7).isShow()) {card_7.setVisibility(View.VISIBLE);} else {card_7.setVisibility(View.GONE);}if (stuList.get(8).isShow()) {card_8.setVisibility(View.VISIBLE);} else {card_8.setVisibility(View.GONE);}if (stuList.get(9).isShow()) {card_9.setVisibility(View.VISIBLE);} else {card_9.setVisibility(View.GONE);}if (stuList.get(10).isShow()) {card_10.setVisibility(View.VISIBLE);} else {card_10.setVisibility(View.GONE);}if (stuList.get(11).isShow()) {card_11.setVisibility(View.VISIBLE);} else {card_11.setVisibility(View.GONE);}}/** 根據 json里面的size 大小  來確定 控件的 長寬* 11 12 21 22 33 */width =  AdaptScreenUtils.getScreenItemWidth();height = AdaptScreenUtils.getScreenItemHeight()* 86 / 100;Log.e("INFO", "##### Sub_size: " + width+": ===" + height);if (stuList.get(0).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_0, stuList.get(0).getLocation(), width,height,width,height);}else if (stuList.get(0).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_0, stuList.get(0).getLocation(), width,height*2,width,height);}else if (stuList.get(0).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_0, stuList.get(0).getLocation(), width*2,height,width,height);}else if (stuList.get(0).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_0, stuList.get(0).getLocation(), width*2,height*2,width,height);}else if (stuList.get(0).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_0, stuList.get(0).getLocation(), width*3,height*3,width,height);}if (stuList.get(1).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_1, stuList.get(1).getLocation(), width,height,width,height);}else if (stuList.get(1).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_1, stuList.get(1).getLocation(), width,height*2,width,height);}else if (stuList.get(1).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_1, stuList.get(1).getLocation(), width*2,height,width,height);}else if (stuList.get(1).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_1, stuList.get(1).getLocation(), width*2,height*2,width,height);}else if (stuList.get(1).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_1, stuList.get(1).getLocation(), width*3,height*3,width,height);}if (stuList.get(2).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_2, stuList.get(2).getLocation(), width,height,width,height);}else if (stuList.get(2).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_2, stuList.get(2).getLocation(), width,height*2,width,height);}else if (stuList.get(2).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_2, stuList.get(2).getLocation(), width*2,height,width,height);}else if (stuList.get(2).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_2, stuList.get(2).getLocation(), width*2,height*2,width,height);}else if (stuList.get(2).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_2, stuList.get(2).getLocation(), width*3,height*3,width,height);}if (stuList.get(3).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_3, stuList.get(3).getLocation(), width,height,width,height);}else if (stuList.get(3).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_3, stuList.get(3).getLocation(), width,height*2,width,height);}else if (stuList.get(3).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_3, stuList.get(3).getLocation(), width*2,height,width,height);}else if (stuList.get(3).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_3, stuList.get(3).getLocation(), width*2,height*2,width,height);}else if (stuList.get(3).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_3, stuList.get(3).getLocation(), width*3,height*3,width,height);}if (stuList.get(4).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_4, stuList.get(4).getLocation(), width,height,width,height);}else if (stuList.get(4).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_4, stuList.get(4).getLocation(), width,height*2,width,height);}else if (stuList.get(4).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_4, stuList.get(4).getLocation(), width*2,height,width,height);}else if (stuList.get(4).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_4, stuList.get(4).getLocation(), width*2,height*2,width,height);}else if (stuList.get(4).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_4, stuList.get(4).getLocation(), width*3,height*3,width,height);}if (stuList.get(5).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_5, stuList.get(5).getLocation(), width,height,width,height);}else if (stuList.get(5).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_5, stuList.get(5).getLocation(), width,height*2,width,height);}else if (stuList.get(5).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_5, stuList.get(5).getLocation(), width*2,height,width,height);}else if (stuList.get(5).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_5, stuList.get(5).getLocation(), width*2,height*2,width,height);}else if (stuList.get(5).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_5, stuList.get(5).getLocation(), width*3,height*3,width,height);}if (stuList.get(6).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_6, stuList.get(6).getLocation(), width,height,width,height);}else if (stuList.get(6).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_6, stuList.get(6).getLocation(), width,height*2,width,height);}else if (stuList.get(6).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_6, stuList.get(6).getLocation(), width*2,height,width,height);}else if (stuList.get(6).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_6, stuList.get(6).getLocation(), width*2,height*2,width,height);}else if (stuList.get(6).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_6, stuList.get(6).getLocation(), width*3,height*3,width,height);}if (stuList.get(7).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_7, stuList.get(7).getLocation(), width,height,width,height);}else if (stuList.get(7).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_7, stuList.get(7).getLocation(), width,height*2,width,height);}else if (stuList.get(7).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_7, stuList.get(7).getLocation(), width*2,height,width,height);}else if (stuList.get(7).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_7, stuList.get(7).getLocation(), width*2,height*2,width,height);}else if (stuList.get(7).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_7, stuList.get(7).getLocation(), width*3,height*3,width,height);}if (stuList.get(8).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_8, stuList.get(8).getLocation(), width,height,width,height);}else if (stuList.get(8).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_8, stuList.get(8).getLocation(), width,height*2,width,height);}else if (stuList.get(8).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_8, stuList.get(8).getLocation(), width*2,height,width,height);}else if (stuList.get(8).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_8, stuList.get(8).getLocation(), width*2,height*2,width,height);}else if (stuList.get(8).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_8, stuList.get(8).getLocation(), width*3,height*3,width,height);}if (stuList.get(9).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_9, stuList.get(9).getLocation(), width,height,width,height);}else if (stuList.get(9).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_9, stuList.get(9).getLocation(), width,height*2,width,height);}else if (stuList.get(9).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_9, stuList.get(9).getLocation(), width*2,height,width,height);}else if (stuList.get(9).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_9, stuList.get(9).getLocation(), width*2,height*2,width,height);}else if (stuList.get(9).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_9, stuList.get(9).getLocation(), width*3,height*3,width,height);}if (stuList.get(10).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_10, stuList.get(10).getLocation(), width,height,width,height);}else if (stuList.get(10).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_10, stuList.get(10).getLocation(), width,height*2,width,height);}else if (stuList.get(10).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_10, stuList.get(10).getLocation(), width*2,height,width,height);}else if (stuList.get(10).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_10, stuList.get(10).getLocation(), width*2,height*2,width,height);}else if (stuList.get(10).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_10, stuList.get(10).getLocation(), width*3,height*3,width,height);}if (stuList.get(11).getSize()==11){AdaptScreenUtils.getScreenHeight5(card_11, stuList.get(11).getLocation(), width,height,width,height);}else if (stuList.get(11).getSize()==12){AdaptScreenUtils.getScreenHeight5(card_11, stuList.get(11).getLocation(), width,height*2,width,height);}else if (stuList.get(11).getSize()==21){AdaptScreenUtils.getScreenHeight5(card_11, stuList.get(11).getLocation(), width*2,height,width,height);}else if (stuList.get(11).getSize()==22){AdaptScreenUtils.getScreenHeight5(card_11, stuList.get(11).getLocation(), width*2,height*2,width,height);}else if (stuList.get(11).getSize()==33){AdaptScreenUtils.getScreenHeight5(card_11, stuList.get(11).getLocation(), width*3,height*3,width,height);}/** 必須想辦法 查看一下控件的大小才行呢* */ViewTreeObserver vto3 = card_1.getViewTreeObserver();vto3.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {int width = card_1.getWidth();int height = card_1.getHeight();// 在獲取到寬高之后要及時移除監聽器,避免重復調用Log.e("TAG","card_1:AutoCardActivity_data:"+width+"==="+height);card_1.getViewTreeObserver().removeOnGlobalLayoutListener(this);}});}return false;}});@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);card_0 = findViewById(R.id.auto_card_0);card_1 = findViewById(R.id.auto_card_1);card_2 = findViewById(R.id.auto_card_2);card_3 = findViewById(R.id.auto_card_3);card_4 = findViewById(R.id.auto_card_4);card_5 = findViewById(R.id.auto_card_5);card_6 = findViewById(R.id.auto_card_6);card_7 = findViewById(R.id.auto_card_7);card_8 = findViewById(R.id.auto_card_8);card_9 = findViewById(R.id.auto_card_9);card_10 = findViewById(R.id.auto_card_10);card_11 = findViewById(R.id.auto_card_11);stuList = new ArrayList<>();loadJsonFromAssests();}public void loadJsonFromAssests() {String json = null;try {InputStream inputStream = getAssets().open("stu0.json");int size = inputStream.available();byte[] buffer = new byte[size];inputStream.read(buffer);inputStream.close();json = new String(buffer, "UTF-8");} catch (IOException ex) {ex.printStackTrace();}Log.i("TAG", "loadJsonFromAssests:" + json.toString());Message msg = new Message();msg.what = 100;msg.obj = json.toString();mHandler.sendMessage(msg);}}

end

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/12421.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/12421.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/12421.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

129.哈希表:有效的字母異位詞(力扣)

242. 有效的字母異位詞 - 力扣&#xff08;LeetCode&#xff09; 題目描述 代碼解決以及思路 這個方法的時間復雜度為O(N)&#xff0c;其中N是字符串的長度&#xff0c;空間復雜度為O(1)&#xff08;因為輔助數組的大小是固定的26&#xff09;。 class Solution { public:bo…

python通過ctypes調用C/C++ SDK,當SDK異常時,同時打印C/C++/Python的棧信息

python通過ctypes調用C/C SDK,當SDK異常時,同時打印C/C/Python的棧信息 一.復現步驟二.輸出 本文演示了python通過ctypes調用C/C SDK,當SDK異常時,同時打印C/C/Python的棧信息.基于traceback、addr2line、PyErr_SetString、backtrace_symbols 一.復現步驟 cat > print_bac…

自媒體的發展趨勢:從個人表達到全球話語權

一、引言隨著數字技術的快速發展&#xff0c;信息傳播的方式和格局也在不斷變化。自媒體&#xff0c;作為其中的一股重要力量&#xff0c;正在以它的獨特方式改變著全球的信息傳播和社會發展。本文將從自媒體的定義及發展歷程入手&#xff0c;深入探討自媒體未來的發展趨勢&…

感知局部規劃--似然場局部規劃

系列文章目錄 提示&#xff1a;這里可以添加系列文章的所有文章的目錄&#xff0c;目錄需要自己手動添加 TODO:寫完再整理 文章目錄 系列文章目錄前言感知導航感知似然場局部規劃&#xff08;很像DWA但是不依賴地圖&#xff0c;完全依賴感知&#xff09; 前言 認知有限&#x…

Uniapp開發入門:構建跨平臺應用的全面指南

引言 什么是Uniapp Uniapp是一款由DCloud公司推出的基于Vue.js的跨平臺應用開發框架。它的核心理念是“一套代碼&#xff0c;多端運行”&#xff0c;開發者只需編寫一份代碼&#xff0c;即可生成包括iOS、Android、H5、微信小程序、支付寶小程序、百度小程序等多平臺的應用。…

初識C++ · string的使用(2)

目錄 1 Modifiers部分 1.1 assign的使用 1.2 insert的使用 1.3 erase的使用 1.4 replace的使用 2 capacity部分 2.1 max_size的使用 2.2 capacity的使用 2.3 reserve的使用 2.4 shrink_to_fit簡介 2.5 resize的使用 2.6 clear的使用 3 String operations部分 3.1 …

[數據結構1.0]快速排序

最近學習了快速排序&#xff0c;鼠鼠俺來做筆記了&#xff01; 本篇博客用排升序為例介紹快速排序&#xff01; 1.快速排序 快速排序是Hoare于1962年提出的一種二叉樹結構的交換排序方法&#xff0c;其基本思想為&#xff1a;任取待排序元素序列中的某元素作為基準值&#x…

202103青少年軟件編程(Python)等級考試試卷(一級)

一、單選題&#xff08;共25題&#xff0c;每題2分&#xff0c;共50分&#xff09; 下列哪個操作不能退出IDLE環境&#xff1f;&#xff08; &#xff09; A、AltF4 B、CtrlQ C、按ESC鍵 D、exit() 試題編號&#xff1a;20210124-yfj-003 題型&#xff1a;單選題 答案&#xf…

Java面試八股之一個char類型變量能不能存儲一個中文字符

Java中一個char類型變量能不能存儲一個中文字符&#xff1f;為什么&#xff1f; Java中一個char類型變量可以存儲一個中文字符。原因如下&#xff1a; Unicode編碼支持&#xff1a;Java語言采用Unicode字符集作為其內建字符編碼方式。Unicode是一種廣泛接受的字符編碼標準&am…

兩小時看完花書(深度學習入門篇)

1.深度學習花書前言 機器學習早期的時候十分依賴于已有的知識庫和人為的邏輯規則&#xff0c;需要人們花大量的時間去制定合理的邏輯判定&#xff0c;可以說是有多少人工&#xff0c;就有多少智能。后來逐漸發展出一些簡單的機器學習方法例如logistic regression、naive bayes等…

mybatisplus查詢練習代碼

mybatisplus查詢練習代碼 package com.yase;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.yase.entity.Student; import com.yase.entity.Teacher; import com.yase…

什么是CCRC?做什么用的?

CCRC&#xff08;中國網絡安全審查認證和市場監管大數據中心&#xff09;原名為中國網絡安全審查技術與認證中心&#xff0c;也被稱為中國信息安全認證中心&#xff08;ISCCC&#xff09;。 該中心是經中央機構編制委員會辦公室批準成立的&#xff0c;其主要職責是依據國家法律…

kafka集群傳統部署(raft模式)—— 筑夢之路

kafka二進制包&#xff1a;https://dlcdn.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz 集群規劃 主機名IP地址節點ID角色分配kafka1192.168.100.1001broker,controllerkafka2192.168.100.1012broker,controllerkafka3192.168.100.1023broker,controller 編輯配置文件 con…

代碼隨想錄算法訓練營第36天|● 738.單調遞增的數字 ● 968.監控二叉樹

738. 單調遞增的數字 發現第一位變小了其他的迅速變9 class Solution:def monotoneIncreasingDigits(self, n: int) -> int:strnlist(str(n))for i in range(len(strn)-1,0,-1):if strn[i-1]>strn[i]:strn[i-1]str(int(strn[i-1])-1)for j in range(i,len(strn)):strn[…

超級簡單的地圖操作工具開發可疑應急,地圖畫點,畫線,畫區域,獲取地圖經緯度等

使用echars的地圖畫點,畫線,畫區域,獲取地圖經緯度等 解壓密碼:10086007 地圖也是用臨時的bmap.js和china.js純離線二選一 一共就這么多文件 畫點,畫線,畫區域 點擊地圖獲取經緯度-打印到控制臺,這樣就能渲染航跡,多變形,結合其他算法算圓等等操作 下載資源:https://download…

JSON-server 服務的搭建

1、全局安裝&#xff1a; pnpm i -g json-server2、創建db.json文件 {"posts": [{"id": 1,"title": "json-server","author": "typicode"}],"comments":[{"id": 1,"body": "…

什么情況下會造成索引失效?

2.3.4. 索引失效 對索引使用左或者左右模糊匹配 使用左或者左右模糊匹配的時候&#xff0c;也就是 like %xx 或者 like %xx% 這兩種方式都會造成索引失效。但是如果前綴是確定的那么就可以使用到索引&#xff0c;例如 name like 許%。 因為索引 B 樹是按照「索引值」有序排列…

SpringBoot 中 zip 文件解壓工具類

SpringBoot 中 zip 文件解壓工具類 zip 文件解壓&#xff08;不支持密碼&#xff09; 相關 Maven 依賴 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version>…

練習題(2024/5/14)

1四數相加 II 給你四個整數數組 nums1、nums2、nums3 和 nums4 &#xff0c;數組長度都是 n &#xff0c;請你計算有多少個元組 (i, j, k, l) 能滿足&#xff1a; 0 < i, j, k, l < nnums1[i] nums2[j] nums3[k] nums4[l] 0 示例 1&#xff1a; 輸入&#xff1a;n…

代碼隨想錄訓練營Day28:貪心算法06

1.738單調遞增的數字 貪心策略&#xff1a;如果strNum[i]<strNum[i-1]那么strNum[i] 9,strNum[i-1]--;//比如87對應的最大的單調遞增的就是79. 具體實現&#xff1a; 對于遇到小于的情況&#xff1a;如果strNum[i]<strNum[i-1]那么strNum[i] 9,strNum[i-1]--;遍歷順…