天天看點

[Android1.6]動态添加View的問題

前言

  小米加步槍,跑步進Android,還沒來得及學習就直接項目了 - - # ,布局這塊仍然是很麻煩的一塊,先記錄些問題吧。

正文

  一、需求

    動态切換View,即把grid.xml中的GridView動态添加到id為ContentView的LinearLayout中。

  二、問題代碼

    2.1  main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:verticalSpacing="0dp"

    android:padding="0dp" android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <LinearLayout android:layout_height="333dp" android:id="@+id/ContentView"

        android:background="#FF0000" android:gravity="fill"

        android:layout_width="fill_parent"></LinearLayout>

    <!-- 底部菜單欄 -->

    <LinearLayout 

        android:background="#000928" android:layout_width="wrap_content"

        android:layout_height="wrap_content" androidrientation="horizontal" android:gravity="fill">

        <ImageView android:src="@drawable/main_info" 

            android:layout_width="wrap_content" android:layout_height="wrap_content" />

        <ImageView android:src="@drawable/main_index" 

        <ImageView android:src="@drawable/main_own" 

        <ImageView android:src="@drawable/main_help" 

        <ImageView android:src="@drawable/main_set" 

        <ImageView android:src="@drawable/main_quit" 

        <ImageView android:src="@drawable/main_return" 

    </LinearLayout>

</LinearLayout>

    2.2  grid.xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/gridview" android:layout_width="fill_parent"

    android:numColumns="5"

    android:verticalSpacing="30dp" android:horizontalSpacing="10dp"

    android:columnWidth="90dp" android:stretchMode="columnWidth"

     android:background="#000928"  android:scrollY = "-50dp"

     android:gravity="center" 

    />

    2.3  背景代碼

    private void ChangeView()

    {

        ly.removeAllViews();

        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.grid,null);

        GridView gridview = (GridView)layout.findViewById(R.id.gridview);

        gridview.setAdapter(new ItemAdapter(MainActivity.this));

        gridview.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,

                    int arg2, long arg3) {

            }

            public void onNothingSelected(AdapterView<?> arg0) {

        });

        ly.addView(gridview);

    }

      代碼說明:

        a).  ly為main.xml中id為ContentView的LinearLayout,即需動态添加View的容器

        b).  ItemAdapter為Grid填充資料的輔助類

    2.4  現象

      2.4.1  正常

      如果把grid.xml中GridView的代碼直接複制粘貼到main.xml中LinearLayout容器内沒有任何問題,布局正常。

      2.4.2  不正常

      如上動态添加android:layout_height="fill_parent"就失效,不管這裡設定絕對數值如300dp也不行,GridView始終隻顯示有Item的内容,即容器内的View無法完全填充LinearLayout父容器。

  三、 解決代碼

    就一行代碼,不知道是Android的Bug還是怎麼:

ly.addView(gridview,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/582251,如需轉載請自行聯系原作者

繼續閱讀