2

我想创造这样的东西

|按钮|

第 1 项

第 2 项

第 3 项

. . .列表视图上的项目

我已经有一个带有 3 个选项卡的 tabhost,所以我不想更改我的 main.xml,因为该按钮将出现在每个选项卡上!我希望我的第一个选项卡显示一个日历(这个已经完成,我不确定它是否可以,但它已经完成),第二个选项卡将显示不同的内容,最后一个选项卡是按钮和下面的项目列表。

我没有粘贴任何代码,因为我所做的一切都来自 android 教程,所以我不想让别人给我一个已经写好的代码,只是为了指导我完成我必须阅读的内容以及在哪里寻找实现那!

提前致谢!

好吧,这就是我到目前为止所做的,这是我的主要课程

`公共类 HourPayActivity 扩展 TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an //setContentView(R.layout.emptab); Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MonthsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Months").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_months))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, EmployersActivity.class);
    spec = tabHost.newTabSpec("Employers").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_employers))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, PaymentsActivity.class);
    spec = tabHost.newTabSpec("Payments").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_payments))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

} `

这是我要显示的标签内容

public class EmployersActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ListView employersList = getListView();

    String[] employers = getResources().getStringArray(R.array.employers_list);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, employers));
    employersList.setAdapter(getListAdapter());       

    employersList.setTextFilterEnabled(true);



    employersList.setOnItemClickListener(new OnItemClickListener() {
        //@Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_LONG).show();
        }
    });

    setContentView(employersList);

}
4

1 回答 1

0

你要做的是;您遵循 android 教程,如果我没记错的话,它会告诉您创建一个扩展 TabActivity 的类。

在此活动中,您可以通过以下方式加载意图

TabHost.TabSpec spec = tabHost.newTabSpec([title]);
....
Intent content = new Intent(this, activityToLoad.class);
spec.setContent(content);
tabHost.addTab(spec);

它的作用是在选项卡中加载一个活动。这反过来意味着您可以像平常一样使用活动,您可以使用 setContentView(R.layout.yourlayout); 的自定义布局;在 onCreate() 方法中。您可以调用您的组件,将自定义列表适配器附加到您的列表等等。为 3 个选项卡创建 3 个不同的活动。根据个人知识,它是维护标签的最简单方法。

您在这些布局中放置的内容取决于您。但是您放入选项卡中的那些活动是您(可能)熟悉的“正常”活动。

解释你的观点:

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
    <Button 
     android:id="@+id/btnId"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/yourtext"
     />
    <ListView 
    android:id="@android:id/android:list"
    android:layout_height="fill_content" 
    android:layout_width="match_parent"
    />
</LinearLayout>

这将在顶部放置一个按钮,在其下方放置一个列表视图。列表视图的 id 是 android:list 因为在我的活动中我扩展了 listActivity,它需要一个具有该 id 的列表视图。

于 2012-05-21T06:40:59.497 回答