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

上传人:b****2 文档编号:11357963 上传时间:2023-05-31 格式:DOCX 页数:5 大小:16.58KB
下载 相关 举报
android中使用代码动态网格布局.docx_第1页
第1页 / 共5页
android中使用代码动态网格布局.docx_第2页
第2页 / 共5页
android中使用代码动态网格布局.docx_第3页
第3页 / 共5页
android中使用代码动态网格布局.docx_第4页
第4页 / 共5页
android中使用代码动态网格布局.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《android中使用代码动态网格布局.docx》由会员分享,可在线阅读,更多相关《android中使用代码动态网格布局.docx(5页珍藏版)》请在冰点文库上搜索。

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

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

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

Android中使用代码动态网格布局标签:

getIdentifierAndroid布局动态布局2015-05-2616:

07

2982人阅读

评论(0)

收藏

举报分类:

Android自定义控件(20)版权声明:

本文为博主原创,转载务必注明出处,谢谢!

目录(?

)[+]

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

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

此处介绍通过java代码进行动态布局。

一、效果图:

图片资源随便找的,将就将就吧

二、给出xml文件布局[html]viewplaincopyprint?

<?

xmlversion="1.0"encoding="utf-8"?

><ScrollViewxmlns:

android="android:

layout_width="match_parent"android:

layout_height="match_parent"android:

background="@android:

color/white"><LinearLayoutandroid:

layout_width="match_parent"android:

layout_height="wrap_content"android:

orientation="vertical"><!

--此处标题栏可以自定义,因为每一个页面都有标题,返回,等等--><RelativeLayoutandroid:

id="@+id/layout_titlebar"android:

layout_width="match_parent"android:

layout_height="48dp"android:

layout_marginBottom="20dp"android:

background="#ed4255"><TextViewandroid:

id="@+id/text_title"style="@style/Text.Title"android:

layout_width="match_parent"android:

layout_height="match_parent"android:

gravity="center"android:

text="业务功能介绍"/></RelativeLayout><!

--子布局由代码动态生成--><LinearLayoutandroid:

id="@+id/layout_more"android:

layout_width="match_parent"android:

layout_height="match_parent"android:

orientation="vertical"android:

padding="4dp"/></LinearLayout></ScrollView>

三、子条目xml布局文件[html]viewplaincopyprint?

<FrameLayoutxmlns:

android="android:

layout_width="match_parent"android:

layout_height="84dp"android:

layout_weight="1.0"android:

clickable="true"><ImageViewandroid:

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"/><TextViewandroid:

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"/></FrameLayout>

如图:

四、java代码动态布局[java]viewplaincopyprint?

/***@authorgao_chun**/publicclassMainActivityextendsActivityimplementsOnClickListener{privateViewGroupmMoreLayout;//父布局容器(动态加载的资源图片和文字等布局都将添加在其里面)/*(non-Javadoc)*@seeapp.ui.TitleActivity#onCreate(android.os.Bundle)*/@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);initUI();//保证启动方法的唯一性}privatevoidinitUI(){setContentView(R.layout.activity_main);//找到该容器(这里的控件为LinearLayout,转换为ViewGroup是因为ViewGroup是容器的基类)mMoreLayout=(ViewGroup)findViewById(R.id.layout_more);//由于文字也是动态生成,使用android中array文件定义资源文件,并取出finalString[]categories=getResources().getStringArray(R.array.categories);finalintsize=categories.length;//String[]的长度finalintrowCount=size/3;//需要布局的行数(每行三个)/***动态添加布局方法封装*参数1.父容器2.资源文字数组3.从第几个开始4.行数*/fillViews(mMoreLayout,categories,0,rowCount);}privatevoidfillViews(ViewGrouplayout,String[]categories,intstart,intend){//表格第一条线View.inflate(this,R.layout.layout_line_horizonal,layout);for(inti=start;i<end;i++){//找到索引,便于根据索引添加图片文件和文字finalintfirstIndex=i*3;finalintsecondIndex=i*3+1;finalintthirdIndex=i*3+2;finalStringfirstCategory=categories[firstIndex];finalStringsecondCategory=categories[secondIndex];finalStringthirdCategory=categories[thirdIndex];//这里控制的是加载本地图片,通过应用包命找到有规则命名的图片资源文件//--->因为这里有两种效果,一是默认的图片,二是按下触发后的图片和文字finalintfirstDrawableNormal=getResources().getIdentifier(String.format("ic_department_%02d_normal",firstIndex+1),"drawable",getApplicationContext().getPackageName());finalintsecondDrawableNormal=getResources().getIdentifier(String.format("ic_department_%02d_normal",secondIndex+1),"drawable",getApplicationContext().getPackageName());finalintthirdDrawableNormal=getResources().getIdentifier(String.format("ic_department_%02d_normal",thirdIndex+1),"drawable",getApplicationContext().getPackageName());finalintfirstDrawablePressed=getResources().getIdentifier(String.format("ic_department_%02d_pressed",firstIndex+1),"drawable",getApplicationContext().getPackageName());finalintsecondDrawablePressed=getResources().getIdentifier(String.format("ic_department_%02d_pressed",secondIndex+1),"drawable",getApplicationContext().getPackageName());finalintthirdDrawablePressed=getResources().getIdentifier(String.format("ic_department_%02d_pressed",thirdIndex+1),"drawable",getApplicationContext().getPackageName());//这里是将上面找到的默认图片和按下时的图片放入到StateListDrawable缓存中finalStateListDrawablefirstDrawable=newStateListDrawable();firstDrawable.addState(newint[]{android.R.attr.state_pressed},getResources().getDrawable(firstDrawablePressed));firstDrawable.addState(newint[]{},getResources().getDrawable(firstDrawableNormal));finalStateListDrawablesecondDrawable=newStateListDrawable();secondDrawable.addState(newint[]{android.R.attr.state_pressed},getResources().getDrawable(secondDrawablePressed));secondDrawable.addState(newint[]{},getResources().getDrawable(secondDrawableNormal));finalStateListDrawablethirdDrawable=newStateListDrawable();thirdDrawable.addState(newint[]{android.R.attr.state_pressed},getResources().getDrawable(thirdDrawablePressed));thirdDrawable.addState(newint[]{},getResources().getDrawable(thirdDrawableNormal));//父布局finalLinearLayoutlinearLayout=newLinearLayout(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,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);LayoutParamslayoutParams=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);layout.addView(linearLayout,layoutParams);//表格最后一条线View.inflate(this,R.layout.layout_line_horizonal,layout);//根据索引getChildAt到指定的位置finalViewfirstView=linearLayout.getChildAt

(1);firstView.setTag(firstCategory);//设置tag,便于在后面判断点击的哪一个firstView.setOnClickListener(this);//设置点击finalTextViewfirstTextView=(TextView)firstView.findViewById(R.id.text_title);firstTextView.setText(firstCategory);//设置文字finalImageViewfirstImageView=(ImageView)firstView.findViewById(R.id.image_icon);firstImageView.setImageDrawable(firstDrawable);//将之前缓存的图片设置出来finalViewsecondView=linearLayout.getChildAt(3);secondView.setTag(secondCategory);secondView.setOnClickListener(this);finalTextViewsecondTextView=(TextView)secondView.findViewById(R.id.text_title);secondTextView.setText(secondCategory);finalImageViewsecondImageView=(ImageView)secondView.findViewById(R.id.image_icon);secondImageView.setImageDrawable(secondDrawable);finalViewthirdView=linearLayout.getChildAt(5);thirdView.setTag(thirdCategory);thirdView.setOnClickListener(this);finalTextViewthirdTextView=(TextView)thirdView.findViewById(R.id.text_title);thirdTextView.setText(thirdCategory);finalImageViewthirdImageView=(ImageView)thirdView.findViewById(R.id.image_icon);thirdImageView.setImageDrawable(thirdDrawable);}}/*(non-Javadoc)*@seeapp.ui.TitleActivity#onClick(android.view.View)*/@OverridepublicvoidonClick(Viewv){finalObjecttag=v.getTag();//通过之前setTag找到点击位置if(tag!

=null){Stringdepartment=(String)tag;Toast.makeText(this,department,0).show();}//elseignored}}

在onClick事件中通过布局时设置的Tag找出用户点击的是哪一个具体的Layout

注:

关于getResources().getIdentifier方法可参考:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

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

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