繁体   English   中英

TabWidget中的Android gridView

[英]Android gridView in TabWidget

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

当我尝试这样的事情:

<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);
}

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

为此,请在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM