Android:自定义选项卡
我决定在我的选项卡界面中使用视图...... 我遵循选项卡界面教程,本教程的结果是我的选项卡下有 4 个没有内容(文本)的选项卡。
我想知道视图如何工作。我如何制定一种方法将内容设置为来自另一个类的选项卡。所以 main.java 是我的带有视图(选项卡)的主文件。 Tab1.java 有谷歌地图导航代码。
如何调用 setupTab 方法并将导航功能设置为选项卡 1。
在这里您可以看到我的代码:
提前致谢!
package CustomTabs;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
public class CustomTabs extends Activity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tabhost
setContentView(R.layout.main);
setupTabHost();
setupTab(new TextView(this), "Tab 1", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 2", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 3", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 4", getResources().getDrawable(R.drawable.ic_tab_artists));
{
final View v = mTabHost.getTabWidget().getChildAt(0);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(1);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(2);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(3);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
}
private void setupTab(final View view, final String tag, Drawable icon) {
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tag, icon).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
}
I have decided to use views for my tab interface...
I follow a tab interface tutorial and the result of this tutorial is 4 tabs without content (text) under my tabs..
I was wondering how views working.. how can I make a method to set content to tab from an another class.. So main.java is my main file with the views (tabs). Tab1.java has google maps navigate code.
How I can invoke the method setupTab and set the navigation function to tab 1.
Here you can see my code:
Thanks in advance!
package CustomTabs;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
public class CustomTabs extends Activity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tabhost
setContentView(R.layout.main);
setupTabHost();
setupTab(new TextView(this), "Tab 1", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 2", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 3", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 4", getResources().getDrawable(R.drawable.ic_tab_artists));
{
final View v = mTabHost.getTabWidget().getChildAt(0);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(1);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(2);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(3);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
}
private void setupTab(final View view, final String tag, Drawable icon) {
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tag, icon).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您想要的吗?基本上,您只需要设置意图并将其与相关选项卡关联即可。
Is this what you want? Basically you just need to set the intent and associate it with the relevant tab.
如果我理解正确,您想要将活动添加为选项卡的内容,因此您需要在 setupTab 方法中添加意图。示例:
这将启动您的选项卡所需的意图。现在,为了使 Intents 可见,您必须将其添加到 setupTab 方法中,如下所示:
如果这不是您正在寻找的内容,那么您需要重申您的问题。
If I understand this right, you want to add your activities as the content of your tabs, there fore you need to add the intent in the setupTab method. Example:
This starts the intents need for your tabs. Now in order for the Intents to be visible you have to add it to your setupTab method like so:
If this is not what you are looking for, then you need to restate your question.