Android 中使用代码动态网格布局,程序员深度学习

本文介绍了如何在Android应用中使用Java代码动态创建网格布局。内容包括设置TextView,LinearLayout,以及动态加载资源图片和文字。通过填充数据并利用StateListDrawable实现点击状态切换。
摘要由CSDN通过智能技术生成

<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 = getRe
sources().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”,

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

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

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

//这里是将上面找到的 默认图片 和 按下时的图片 放入到 StateListDrawable缓存中

final StateListDrawable firstDrawable = new StateListDrawable();

firstDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(firstDrawablePressed));

firstDrawable.addState(new int[]{}, getResources().getDrawable(firstDrawableNormal));

final StateListDrawable secondDrawable = new StateListDrawable();

secondDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(secondDrawablePressed));

secondDrawable.addState(new int[]{}, getResources().getDrawable(secondDrawableNormal));

final StateListDrawable thirdDrawable = new StateListDrawable();

thirdDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(thirdDrawablePressed));

thirdDrawable.addState(new int[]{}, getResources().getDrawable(thirdDrawableNormal));

// 父布局

final LinearLayout linearLayout = new LinearLayout(this);

// 第一个子布局

View.inflate(this, R.layout.layout_line_vertical, linearLayout);

View.inflate(this, R.layout.layout_department, linearLayout);

View.inflate(this, R.layout.layout_line_vertical, linearLayout);

DrawableNormal));

// 父布局

final LinearLayout linearLayout = new LinearLayout(this);

// 第一个子布局

View.inflate(this, R.layout.layout_line_vertical, linearLayout);

View.inflate(this, R.layout.layout_department, linearLayout);

View.inflate(this, R.layout.layout_line_vertical, linearLayout);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值