在做項目的時候,遇到了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裡面叫打氣筒哈,就是將布局一個個打上去。