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.mzph.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]--;遍历顺…