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/p/3813793.html,如需转载请自行联系原作者

相关文章
|
8月前
|
XML 前端开发 Android开发
Android XML 布局基础(四)内外边距(margin、padding)
Android XML 布局基础(四)内外边距(margin、padding)
167 0
|
8月前
|
编解码 Android开发
Android 常用布局单位区别(dp、sp、px、pt、in、mm)
Android 常用布局单位区别(dp、sp、px、pt、in、mm)
294 0
|
4月前
|
Android开发
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
115 0
|
4月前
|
Android开发 容器
Android开发,学习LinearLayout布局
Android开发,学习LinearLayout布局
38 0
|
4月前
|
XML Java Android开发
Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)
Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)
36 0
|
4月前
|
XML Java Android开发
Android Studio App开发中工具栏Toolbar、溢出菜单OverflowMenu、标签布局TabLayout的讲解及实战(实现京东App的标签导航栏,附源码)
Android Studio App开发中工具栏Toolbar、溢出菜单OverflowMenu、标签布局TabLayout的讲解及实战(实现京东App的标签导航栏,附源码)
55 0
|
4月前
|
Android开发 容器
Android开发第二次课 布局方式
Android开发第二次课 布局方式
23 0
|
6月前
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(一)
android 前端常用布局文件升级总结(一)
|
8月前
|
Android开发
Android 使用DataBinding时 将布局页面转换为数据绑定布局(Convert to data binding layout) 不出现提示解决办法
Android 使用DataBinding时 将布局页面转换为数据绑定布局(Convert to data binding layout) 不出现提示解决办法
89 0
|
8月前
|
Android开发
Android 实现布局的缩小和再放大动画(使用scale动画效果进行实现)
Android 实现布局的缩小和再放大动画(使用scale动画效果进行实现)
571 0