首页 > 代码库 > Android -- 动态添加布局

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 = http://www.mamicode.com/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