ImageVerifierCode 换一换
格式:DOCX , 页数:5 ,大小:16.58KB ,
资源ID:11357963      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-11357963.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(android中使用代码动态网格布局.docx)为本站会员(b****2)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

android中使用代码动态网格布局.docx

1、android中使用代码动态网格布局Android 中使用代码动态网格布局 Android 中使用代码动态网格布局标签:getIdentifierAndroid布局动态布局2015-05-26 16:072982人阅读 评论(0) 收藏 举报分类:Android自定义控件(20)版权声明:本文为博主原创,转载务必注明出处,谢谢!目录(?)+Android 中使用代码动态网格布局本文介绍在android中使用代码动态布局,有时候根据不同的需求,比如需要根据服务器上的条目个数来决定app中页面布局控件(显示个数,图标等)。此处介绍通过java代码进行动态布局。一、效果图:图片资源随便找的,将就将就

2、吧二、给出xml文件布局html view plain copy print?<?xml version=1.0 encoding=utf-8?> <ScrollView xmlns: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

3、_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

4、style=style/Text.Title android:layout_width=match_parent android:layout_height=match_parent android:gravity=center android:text=业务功能介绍 /> </RelativeLayout> <!- 子布局由代码动态生成 -> <LinearLayout android:id=+id/layout_more android:layout_width=match_parent android:layout_height=match_paren

5、t android:orientation=vertical android:padding=4dp /> </LinearLayout> </ScrollView> 三、子条目xml布局文件html view plain copy print?<FrameLayout xmlns:android= android:layout_width=match_parent android:layout_height=84dp android:layout_weight=1.0 android:clickable=true > <ImageView an

6、droid: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:layo

7、ut_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 /> </Fra

8、meLayout> 如图:四、java代码动态布局java view plain copy print?/* * author gao_chun * */ public class MainActivity extends Activity implements OnClickListener private ViewGroup mMoreLayout; /父布局容器(动态加载的资源图片和文字等布局都将添加在其里面) /* (non-Javadoc) * see app.ui.TitleActivity#onCreate(android.os.Bundle) */ Override pr

9、otected 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); /由于文字也是动态生成,使用a

10、ndroid中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); pri

11、vate 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 +

12、2; final String firstCategory = categoriesfirstIndex; final String secondCategory = categoriessecondIndex; final String thirdCategory = categoriesthirdIndex; /这里控制的是加载本地图片,通过应用包命找到 有规则命名的图片资源文件 /->因为这里有两种效果,一是默认的图片,二是按下触发后的图片和文字 final int firstDrawableNormal = getResources().getIdentifier(String.

13、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 thirdDrawableNorma

14、l = 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().getPac

15、kageName(); 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

16、+ 1),drawable,getApplicationContext().getPackageName(); /这里是将上面找到的 默认图片 和 按下时的图片 放入到 StateListDrawable缓存中 final StateListDrawable firstDrawable = new StateListDrawable(); firstDrawable.addState(new intandroid.R.attr.state_pressed, getResources().getDrawable(firstDrawablePressed); firstDrawable.addSt

17、ate(new int, getResources().getDrawable(firstDrawableNormal); final StateListDrawable secondDrawable = new StateListDrawable(); secondDrawable.addState(new intandroid.R.attr.state_pressed, getResources().getDrawable(secondDrawablePressed); secondDrawable.addState(new int, getResources().getDrawable(

18、secondDrawableNormal); final StateListDrawable thirdDrawable = new StateListDrawable(); thirdDrawable.addState(new intandroid.R.attr.state_pressed, getResources().getDrawable(thirdDrawablePressed); thirdDrawable.addState(new int, getResources().getDrawable(thirdDrawableNormal); / 父布局 final LinearLay

19、out 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); / 第二个子布局 View.inflate(this, R.layout.layout_department,

20、linearLayout); 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); LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,

21、LayoutParams.WRAP_CONTENT); layout.addView(linearLayout, layoutParams); / 表格最后一条线 View.inflate(this, R.layout.layout_line_horizonal, layout); /根据索引getChildAt到指定的位置 final View firstView = linearLayout.getChildAt(1); firstView.setTag(firstCategory); /设置tag,便于在后面判断点击的哪一个 firstView.setOnClickListener(th

22、is); /设置点击 final TextView firstTextView = (TextView) firstView.findViewById(R.id.text_title); firstTextView.setText(firstCategory); /设置文字 final ImageView firstImageView = (ImageView) firstView.findViewById(R.id.image_icon); firstImageView.setImageDrawable(firstDrawable); /将之前缓存的图片设置出来 final View sec

23、ondView = linearLayout.getChildAt(3); secondView.setTag(secondCategory); secondView.setOnClickListener(this); final TextView secondTextView = (TextView) secondView.findViewById(R.id.text_title); secondTextView.setText(secondCategory); final ImageView secondImageView = (ImageView) secondView.findView

24、ById(R.id.image_icon); secondImageView.setImageDrawable(secondDrawable); final View thirdView = linearLayout.getChildAt(5); thirdView.setTag(thirdCategory); thirdView.setOnClickListener(this); final TextView thirdTextView = (TextView) thirdView.findViewById(R.id.text_title); thirdTextView.setText(th

25、irdCategory); final ImageView thirdImageView = (ImageView) thirdView.findViewById(R.id.image_icon); thirdImageView.setImageDrawable(thirdDrawable); /* (non-Javadoc) * see app.ui.TitleActivity#onClick(android.view.View) */ Override public void onClick(View v) final Object tag = v.getTag(); /通过之前setTag找到点击位置 if (tag != null) String department = (String) tag; Toast.makeText(this, department, 0).show(); / else ignored 在onClick事件中通过布局时设置的Tag找出用户点击的是哪一个具体的Layout注:关于 getResources().getIdentifier 方法可参考:

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2