【发布时间】:2012-05-20 22:43:57
【问题描述】:
我想创造这样的东西
|按钮|
项目 1
项目 2
项目 3
。 . .列表视图上的项目
我已经有一个带有 3 个标签的标签主机,所以我不想更改我的 main.xml,因为该按钮会出现在每个标签上!我希望我的第一个选项卡显示一个日历(这个已经完成,我不确定它是否可以,但它已经完成),第二个选项卡将显示不同的内容,最后一个选项卡是按钮和下面的项目列表。
我没有粘贴任何代码,因为我所做的一切都来自 android 教程,所以我不想让别人给我一个已经写好的代码,只是为了指导我了解我必须阅读的内容以及在哪里阅读期待实现这一目标!
提前致谢!
这就是我到目前为止所做的 这是我的主要课程
`公共类 HourPayActivity 扩展 TabActivity { 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(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);
}
【问题讨论】:
-
我们需要知道您是如何实现这些选项卡的...您是否为每个选项卡使用了一个活动?你在使用碎片吗?您是否为每个片段使用带有活动的片段?您甚至没有提供足够的信息来开始回答您的问题。哦,您关于将按钮添加到主 xml 的断言是不正确的。您可以设置一个主布局 xml,并为其中编码的每个选项卡提供一个单独的视图(同样,这取决于您如何处理选项卡)。
-
感谢您的回答,我已经使用关于 Hello 视图的 Android 教程创建了标签 -> 标签视图。所以我认为它们是使用每个选项卡的不同活动创建的!对不起我的非常“快速”的问题,但我是 android 上的新手:P
-
只是一些小技巧,可以使用Intent的构造函数来传递一个类名。它为您节省了一个方法调用。新意图(this,activity.class);例如。此外,将“扩展 Activity”更改为“扩展 ListActivity”您可以通过调用 getListView().setOnItemClickListener() 将 onClickListener 添加到 ListActivity 中的列表视图