如何在相同的布局中重现GridView?

时间:2015-03-09 11:37:27

标签: android gridview layout-inflater

我有GridView来显示一些图像。我想重现gridview,但我不能那样做。我看到第一个gridview,但我没有看到第二个。要重现GridView,我使用LayoutInflater。 LayoutInflater效果很好,因为我看到屏幕上的重复部分,但GridView不重复。

有人能说我为什么不显示GridView吗?

这是我的MainActivity:

public class MainActivity extends Activity {
ViewGroup parent;
static final String[] numbers = new String[] { 
    "A", "B", "C", "D", "E",
    "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout rl = (LinearLayout) findViewById(R.id.outer);

    LayoutInflater layoutInflater = (LayoutInflater) 
                this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    for(int i =1; i<3; i++){
        rl.addView(layoutInflater.inflate(R.layout.main2, parent, false), i );

        GridView gridView = (GridView) findViewById(R.id.gridView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, numbers);
        gridView.setAdapter(adapter);}

    }
}

这是我的主要布局:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/outer"
>

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Main Layout Content"/>

这是具有GridView的子布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
android:id="@+id/outer">

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Child Layout Content."/>
<GridView
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

enter image description here

谢谢你的帮助。

0 个答案:

没有答案