Android -- 動態添加布局

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.