Android 中使用代码动态网格布局(2)

本文介绍在android中使用代码动态布局,有时候根据不同的需求,比如需要根据服务器上的条目个数来决定app中页面布局控件(显示个数,图标等)。此处介绍通过java代码进行动态布局。

一、效果图:

======

图片资源随便找的,将就将就吧外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

二、给出xml文件布局

===========

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“@android:color/white” >

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“vertical” >

<RelativeLayout

android:id=“@+id/layout_titlebar”

android:layout_width=“match_parent”

android:layout_height=“48dp”

android:layout_marginBottom=“20dp”

android:background=“#ed4255” >

<TextView

android:id=“@+id/text_title”

style=“@style/Text.Title”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:gravity=“center”

android:text=“业务功能介绍” />

<LinearLayout

android:id=“@+id/layout_more”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

android:padding=“4dp” />

三、子条目xml布局文件

============

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“84dp”

android:layout_weight=“1.0”

android:clickable=“true” >

<ImageView

android:id=“@+id/image_icon”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center_horizontal”

android:layout_marginTop=“16dp”

android:duplicateParentState=“true”

android:src=“@drawable/ic_department_01_normal” />

<TextView

android:id=“@+id/text_title”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center_horizontal|bottom”

android:background=“@null”

android:layout_marginBottom=“6dp”

android:gravity=“center”

android:duplicateParentState=“true”

android:textColor=“@drawable/text_service_color”

android:textSize=“14dp” />

如图:

四、java代码动态布局

============

/**

  • @author gao_chun

*/

public class MainActivity extends Activity implements OnClickListener{

private ViewGroup mMoreLayout; //父布局容器(动态加载的资源图片和文字等布局都将添加在其里面)

/* (non-Javadoc)

  • @see app.ui.TitleActivity#onCreate(android.os.Bundle)

*/

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

initUI(); //保证启动方法的唯一性

}

private void initUI() {

setContentView(R.layout.activity_main);

//找到该容器(这里的控件为LinearLayout,转换为ViewGroup是因为ViewGroup是容器的基类)

mMoreLayout = (ViewGroup) findViewById(R.id.layout_more);

//由于文字也是动态生成,使用android中array文件定义资源文件,并取出

final String[] categories = getResources().getStringArray(R.array.categories);

final int size = categories.length; //String[]的长度

final int rowCount = size / 3; //需要布局的行数(每行三个)

/**

  • 动态添加布局方法封装

  • 参数 1.父容器 2.资源文字数组 3.从第几个开始 4.行数

*/

fillViews(mMoreLayout, categories, 0, rowCount);

}

private void fillViews(ViewGroup layout, String[] categories, int start, int end) {

// 表格第一条线

View.inflate(this, R.layout.layout_line_horizonal, layout);

for (int i = start; i < end; i++) {

//找到索引,便于根据索引添加图片文件和文字

final int firstIndex = i * 3;

final int secondIndex = i * 3 + 1;

final int thirdIndex = i * 3 + 2;

final String firstCategory = categories[firstIndex];

final String secondCategory = categories[secondIndex];

final String thirdCategory = categories[thirdIndex];

//这里控制的是加载本地图片,通过应用包命找到 有规则命名的图片资源文件

//—>因为这里有两种效果,一是默认的图片,二是按下触发后的图片和文字

final int firstDrawableNormal = getResources().getIdentifier(String.format(“ic_department_%02d_normal”,

firstIndex + 1),“drawable”,getApplicationContext().getPackageName());

final int secondDrawableNormal = getResources().getIdentifier(String.format(“ic_department_%02d_normal”,

secondIndex + 1),“drawable”,getApplicationContext().getPackageName());

final int thirdDrawableNormal = getResources().getIdentifier(String.format(“ic_department_%02d_normal”,

thirdIndex + 1),“drawable”,getApplicationContext().getPackageName());

final int firstDrawablePressed = getResources().getIdentifier(String.format(“ic_department_%02d_pressed”,

firstIndex + 1),“drawable”,getApplicationContext().getPackageName());

final int secondDrawablePressed = getResources().getIdentifier(String.format(“ic_department_%02d_pressed”,

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

都说三年是程序员的一个坎,能否晋升或者提高自己的核心竞争力,这几年就十分关键。

技术发展的这么快,从哪些方面开始学习,才能达到高级工程师水平,最后进阶到Android架构师/技术专家?我总结了这 5大块;

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 PDF(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

Java语言与原理;
大厂,小厂。Android面试先看你熟不熟悉Java语言

高级UI与自定义view;
自定义view,Android开发的基本功。

性能调优;
数据结构算法,设计模式。都是这里面的关键基础和重点需要熟练的。

NDK开发;
未来的方向,高薪必会。

前沿技术;
组件化,热升级,热修复,框架设计

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

我在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多

当然,想要深入学习并掌握这些能力,并不简单。关于如何学习,做程序员这一行什么工作强度大家都懂,但是不管工作多忙,每周也要雷打不动的抽出 2 小时用来学习。

不出半年,你就能看出变化!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

架设计

[外链图片转存中…(img-jtzJQEq5-1712514804663)]

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

我在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多

当然,想要深入学习并掌握这些能力,并不简单。关于如何学习,做程序员这一行什么工作强度大家都懂,但是不管工作多忙,每周也要雷打不动的抽出 2 小时用来学习。

不出半年,你就能看出变化!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 28
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值