1

我正在尝试在我的 Android tabWidget 中使用简单的 gridView 填充字符串。我有一个带有三个选项卡的 tabHost,每个选项卡都有一个 XML 文件中描述的线性布局。我尝试在其下方设置一个按钮和网格。实现这一目标的最简单方法是什么?

当我尝试这样的事情时:

<LinearLayout
    android:id="@+id/tab2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
<Button 
    android:id ="@+id/downloadBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="pobierz waluty"
/>

<GridView
    android:id="@+id/grid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:verticalSpacing="35px"
    android:horizontalSpacing="5px"
    android:numColumns="auto_fit"
    android:columnWidth="100px"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

在 Java 中:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

    downloadXML = (Button)findViewById(R.id.downloadBtn);

    GridView grid= (GridView)findViewById(R.id.grid);
    grid.setAdapter(new ArrayAdapter<String>   (this,android.R.layout.simple_list_item_1, items));

    TabHost tabs = (TabHost)findViewById(R.id.tabHost);
    tabs.setup();

    TabHost.TabSpec spec = tabs.newTabSpec("tag1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("kalkulator");
    tabs.addTab(spec);

    spec= tabs.newTabSpec("tag2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("kurs walut");
    tabs.addTab(spec);

    spec=tabs.newTabSpec("tag3");
    spec.setContent(R.id.tab3);
    spec.setIndicator("konwerter");
    tabs.addTab(spec);
}

我的第二个标签中只有按钮可见。

4

1 回答 1

1

为此使用下面的XML,它将解决您的问题

<LinearLayout
    android:id="@+id/tab2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button 
        android:id ="@+id/downloadBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="pobierz waluty"/>

    <GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:verticalSpacing="35px"
        android:horizontalSpacing="5px"
        android:numColumns="auto_fit"
        android:columnWidth="100px"
        android:stretchMode="columnWidth"
        android:gravity="center" />

</LinearLayout>
于 2012-12-22T05:32:10.150 回答