0
点赞
收藏
分享

微信扫一扫

Android动态布局framelayout

zibianqu 2024-05-10 阅读 6

功能说明 最近碰到一个需求,要求在网页端拖控件,动态配置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 效果图 screenshot-20240510-204822.png 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

举报

相关推荐

0 条评论