Android动态布局framelayout

news2024/11/17 13:48:05

功能说明
最近碰到一个需求,要求在网页端拖控件,动态配置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">

    <application
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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.CardView
        android: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;


    @Override
    public 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() {
        @Override
        public 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() {
                    @Override
                    public 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;
        }
    });

    @Override
    protected 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.coloradmin.cn/o/1662759.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STL——queue容器【队列】

queue的基本概念&#xff1a; 概念&#xff1a;queue是一种先进先出的数据结构&#xff0c;它有两个出口 queue的构造函数&#xff1a; 构造函数&#xff1a; queue<T>que:采用模板类实现&#xff0c;queue对象的默认构造形式 queue(const queue &que):拷贝构造函…

SpringBoot项目代码反编译、加密

话不多说&#xff0c;直接上干活 1. 引入依赖地址和依赖 在maven的setting.xml中添加 <repositories><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository> </repositories>在需要加密的项目的pom.x…

ORACLE ODA一体机存储节点电源故障的分析处理

近期&#xff0c;某用户的ORACLE ODA一体机在例行机房巡检时出现亮黄灯告警&#xff1b;用户反馈次问题后我们立刻通过远程方式&#xff0c;登陆ODA的控制台进行查看&#xff1b; 对于ODA一体机&#xff08;2个计算节点1个存储节点&#xff09;&#xff0c;计算节点可以通过il…

LabVIEW MEMS电容式压力传感器测试系统

LabVIEW MEMS电容式压力传感器测试系统 随着微电子技术的发展&#xff0c;MEMS&#xff08;微电机系统&#xff09;技术在各个领域得到了广泛应用。MEMS电容式压力传感器以其高灵敏度、小尺寸、低功耗等优点&#xff0c;在微传感器领域占据了重要的地位。然而&#xff0c;这些…

【Spring】Spring 整合 Junit、MyBatis

一、 Spring 整合 Junit <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache…

Wallace树乘法器及Verilog实现

一、Wallace树乘法器 Wallace树乘法器就是将多个部分积进行分组&#xff0c;每三个一组&#xff0c;最后如果剩下的部分积个数不够三个的不做处理&#xff0c;然后将各组的部分积进行相加得到和以及进位信息&#xff0c;直到最终只剩下两行部分积&#xff0c;相加后得到最终结…

作为网络安全工程师需要掌握的安全小知识!

网络安全风险无处不在&#xff0c;今天为大家梳理了一些网络安全相关的小知识&#xff0c;希望能进一步提升大家的安全意识&#xff0c;帮助大家建立更加安全的网络环境。 一、主机电脑安全 1、操作系统安全&#xff1a;安装操作系统时需要选择合适的版本&#xff0c;及时打补…

RabbitMQ高级(MQ的问题,消息可靠性,死信交换机,惰性队列,MQ集群)【详解】

目录 一、MQ的问题 1. 问题说明 2. 准备代码环境 1 创建project 2 创建生产者模块 3 创建消费者模块 二、消息可靠性 1. 介绍 2. 生产者确认机制 3. MQ消息持久化 4. 消费者确认机制 5. 消费者auto模式的失败重试 6. 小结 三、死信交换机和延迟消息 1. 介绍 2. …

李彦宏回顾大模型重构百度这一年

“大模型我们走在最前面&#xff0c;我们需要去勇闯无人区&#xff0c;需要去冒前人没有冒过的风险。”近日&#xff0c;在百度一场内部颁奖活动中&#xff0c;百度创始人、董事长兼首席执行官李彦宏指出&#xff0c;百度一直坚信技术可以改变世界&#xff0c;会一直沿着这条路…

【Web】CTFSHOW 七夕杯 题解

目录 web签到 easy_calc easy_cmd web签到 CTF中字符长度限制下的命令执行 rce(7字符5字符4字符)汇总_ctf中字符长度限制下的命令执行 5个字符-CSDN博客7长度限制直接梭了 也可以打临时文件RCE import requestsurl "http://4ae13f1e-8e42-4afa-a6a6-1076acd08211.c…

学习笔记——字符串(单模+多模+练习题)

单模匹配 Brute Force算法&#xff08;暴力&#xff09; 算法思想 母串和模式串字符依次配对&#xff0c;如果配对成功则继续比较后面位置是否相同&#xff0c;如果出现匹配不成功的位置&#xff0c;则j&#xff08;模式串当前的位置&#xff09;从头开始&#xff0c;i&…

【JavaScript】内置对象 - 数组对象 ⑤ ( 数组转字符串 | toString 方法 | join 方法 )

文章目录 一、数组转字符串1、数组转字符串 ( 逗号分割 ) - toString()2、数组转字符串 ( 自定义分割符 ) - join() Array 数组对象参考文档 : https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array 一、数组转字符串 1、数组转字符串 ( 逗…

每日一练 2024.5.10

题目&#xff1a; 给定一个非负整数数组 nums&#xff0c; nums 中一半整数是 奇数 &#xff0c;一半整数是 偶数 。 对数组进行排序&#xff0c;以便当 nums[i] 为奇数时&#xff0c;i 也是 奇数 &#xff1b;当 nums[i] 为偶数时&#xff0c; i 也是 偶数 。 示例 1&#…

景联文科技:用高质量数据采集标注赋能无人机技术,引领无人机迈入新纪元!

随着无人机技术的不断发展与革新&#xff0c;它已成为现代社会中一个前景无限的科技领域。 无人机应用领域 边境巡逻与安防&#xff1a;边境管理部门利用无人机监控边境线&#xff0c;防止非法越境和其他安全威胁&#xff0c;同时也能监控地面安保人员的工作状态和行动路线。 …

双目相机标定流程(MATLAB)

一&#xff1a;经典标定方法 1.1OPENCV 1.2ROS ROS进行双目视觉标定可以得到左右两个相机的相机矩阵和畸变系数&#xff0c;如果是单目标定&#xff0c;用ROS会非常方便。 3.MATLAB标定&#xff08;双目标定&#xff09; MATLAB用来双目标定会非常方便&#xff0c;主要是为…

【oj题】环形链表

目录 一. OJ链接&#xff1a; 环形链表 【思路】 快慢指针 ​编辑【扩展问题】 为什么快指针每次走两步&#xff0c;慢指针走一步可以解决问题&#xff1f; ​编辑【扩展问题】快指针一次走3步&#xff0c;走4步&#xff0c;...n步行吗&#xff1f; 二. OJ链接&#xff1a…

2024年第九届“数维杯”大学生数学建模挑战赛B题

第一个问题为&#xff1a;正己烷不溶物对热解产率是否产生显著影响&#xff1f; 第一个问题&#xff1a;正己烷不溶物(INS)对热解产率是否产生显著影响&#xff1f; 解析&#xff1a;正己烷不溶物(INS)主要是由生物质和煤中的非挥发性物质组成&#xff0c;它们在共热解过程中会…

Django 静态文件管理与部署指南

title: Django 静态文件管理与部署指南 date: 2024/5/10 17:38:36 updated: 2024/5/10 17:38:36 categories: 后端开发 tags: WebOptCDN加速DjangoCompressWebpackStaticDeployCICD-ToolsSecStatic 第一章&#xff1a;介绍 Django 静态文件的概念和重要性 在 Web 开发中&a…

【Docker】Docker部署Java程序

Maven中使用打包插件 <build><finalName>duanjian</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass…

一次完整的GC流程

Java堆中内存区分 Java的堆由新生代&#xff08;Young Generation&#xff09;和老年代&#xff08;Old Generation&#xff09;组成。新生代存放新分配的对象&#xff0c;老年代存放长期存在的对象。 新生代&#xff08;Young&#xff09;由年轻区&#xff08;Eden&a…