Android -- 动态添加布局
在做项目的时候,遇到了scrollView与listView结合的使用,导致了滑动的混乱,但是有一个办法可以解决掉这个问题,就是手写listView的高度,还有另外一种方法,传送门:《Android -- 在ScrollView中嵌套ListView》。
但是在项目中,我们的scrollview中嵌套这两个ListView,这就更麻烦了,为了不去用两个上述方法,我们将另外一个ListView改写为动态加载布局的方法来实现,在布局等操作上感觉还是跟listview差不多,但是没有Adapter。
子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/action_settings"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" /> </LinearLayout>
显示布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <LinearLayout
android:id="@+id/lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView> <Button
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click_add"
android:text="@string/app_name" /> </LinearLayout>
代码实现
public class MainActivity extends Activity {
private LinearLayout lay;
private LinearLayout item;
private ImageView img;
private TextView txt;
private Button btn_add;
private int[] pic = { R.drawable.ic_launcher, R.drawable.maps,
R.drawable.appstore, R.drawable.calculator, R.drawable.camera };
private String[] str_pic = { "ic_launcher", "maps", "appstore",
"calculator", "camera" };
private String[] str = { "1", "2", "3", "4", "5" };
private int time = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lay = (LinearLayout) findViewById(R.id.lay);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
inflateAndFind();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
}
private void inflateAndFind() throws Exception {
item = (LinearLayout) View.inflate(getBaseContext(), R.layout.item,
null);
img = (ImageView) item.findViewById(R.id.img);
txt = (TextView) item.findViewById(R.id.txt);
if (time < 5) {
Class<com.yydcdut.layout.R.drawable> cls = R.drawable.class;
int value = cls.getDeclaredField(str_pic[time]).getInt(null);
// img.setImageResource(pic[time]);
img.setImageResource(value);
txt.setText(str[time]);
lay.addView(item);
} else
time = 0;
time++;
}
}
代码解析
其实运用的方法就是通过inflate方法将新添加的布局一个个添加上去,inflate在Android里面叫打气筒哈,就是将布局一个个打上去。
后面还有个Class<com.yydcdut.layout.R.drawable>,这个是通过名字去获取ID的int值,应该就是Java的反射机制吧~
我是天王盖地虎的分割线
源代码:http://pan.baidu.com/s/1dD1Qx01
layout.zip
转载请注明出处:http://www.cnblogs.com/yydcdut
Android -- 动态添加布局的更多相关文章
- Android动态添加布局
//1.利用LayoutInflater的inflate动态加载XML mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID) ...
- Android动态添加和移除布局
package com.hyang.administrator.studentproject; import android.os.Bundle; import android.support.v7. ...
- Android动态添加Device Admin权限
/********************************************************************** * Android动态添加Device Admin权限 ...
- 动态添加布局、动态添加View、LinearLayout动态添加View;
LinearLayout提供了几个方法,用作动态添加View特别好用: 可以添加View.删除View.删除指定位置View.删除全部View: 看代码: public class MainActiv ...
- Android 动态添加Spinner(.java文件内实现) 实现 改变spinner 内文字属性
动态添加spinner 控件 Spinner s = new Spinner(this); String []items={"自己定义的要显示的数组"}; my_SpinnerAd ...
- Android 动态改变布局属性RelativeLayout.LayoutParams.addRule()
我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...
- android动态添加TextView或者ImageView
动态添加 text1=new TextView(this); text1.setText("动态添加"); ((LinearLayout) this.findViewById(R. ...
- Android 动态添加线性布局(.java文件内) 实现控件按比例分割空间
这里实现 两个 编辑框同一水平上 按1:1分割空间 这里的1:1 比例可以通过 lp1.weight : 1p2.weight =m:n 实现 { LinearLayout l=new Linea ...
- Android 动态添加删除ExpandableListView的item的例子
这个例子可以学习到如下几点: 1.通过自定义Dialog(单独布局的xml文件进行弹出显示) 2.通过menu点击监听添加,删除view中的items 3.点击ExpandableListView中g ...
随机推荐
- ref:spring-data-XMLBean XXE复现分析
ref:https://blog.spoock.com/2018/05/16/cve-2018-1259/ 漏洞信息 看pivotal发布的漏洞信息如下 通过发布的漏洞信息可以知道,漏洞组件是在XML ...
- 洛谷P2464 [SDOI2008] 郁闷的小j [分块]
题目传送门 郁闷的小j 题目描述 小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的危险,这也 ...
- NOIP2013 D1 T3 货车运输
好吧,遇上这种题,作为蒟蒻的我第一个想到的就是怎么打暴力,然而暴力都打不好QAQ!!!于是只能等教练讲解以后,然后在大犇的指导下终于做出来了. 对了,,好像还,没上题....: 题目描述 A 国有 n ...
- Windows群集之NLB
转http://www.aixchina.net/Article/31746 网络负载平衡群集(Network Load balancing) 在Internet快速发展的今天,为了满足企业的高速发展 ...
- 关于urllib、urllib2爬虫伪装的总结
站在网站管理的角度,如果在同一时间段,大家全部利用爬虫程序对自己的网站进行爬取操作,那么这网站服务器能不能承受这种负荷?肯定不能啊,如果严重超负荷则会时服务器宕机(死机)的,对于一些商业型的网站,宕机 ...
- React Native 系列(九)
前言 本系列是基于React Native版本号0.44.3写的.很多的App都使用了Tab标签组件,例如QQ,微信等等,就是切换不同的选项,显示不同的内容.那么这篇文章将介绍RN中的Tab标签组件. ...
- 【BZOJ 2749】 2749: [HAOI2012]外星人 (数论-线性筛?类积性函数)
2749: [HAOI2012]外星人 Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Ou ...
- 【刷水-贪心】BZOJ1629-[Usaco2007 Demo]Cow Acrobats
[题目大意] 有n个头牛,给出体重和力量.每个牛的危险值等于它上面的牛的体重总和减去它的力量值,求所有方案中危险值最大值的最小值. [思路] 贪心.一开始脑补的贪心是体重大的先放下面,体重相同的根据力 ...
- 按考分对学生排序 Exercise08_03
/** * @author 冰樱梦 * 时间:2018年12月 * 题目:按考分对学生排序 * */ public class Exercise08_03 { public static void m ...
- bzoj 4766: 文艺计算姬 -- 快速乘
4766: 文艺计算姬 Time Limit: 1 Sec Memory Limit: 128 MB Description "奋战三星期,造台计算机".小W响应号召,花了三星期 ...