gpt4 book ai didi

java - Android 选项卡 View

转载 作者:行者123 更新时间:2023-12-01 05:51:07 26 4
gpt4 key购买 nike

我决定在我的选项卡界面中使用 View ...我遵循选项卡界面教程,本教程的结果是我的选项卡下有 4 个没有内容(文本)的选项卡..

我想知道 View 是如何工作的..我怎样才能制定一种方法来将内容设置为另一个类的选项卡..所以main.java是我的带有 View (选项卡)的主文件。 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);

}
}

最佳答案

您需要创建一些 Activity 来执行此操作。我的示例采用了 2 个 Activity,第一个 Activity 将扩展 TabActivity,第二个 Activity 将扩展 Activity。

这是我的简单示例:

  • 扩展 TabActivity 的 Activity

package com.bsoft.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import com.bsoft.shared.Shared;

public class ViewActivity extends TabActivity {

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);

String[] strVal = this.getIntent().getExtras().getString("content").split(Shared.SPACE);
setTitle(strVal[1]);

Intent paliIntent = new Intent(this, PaliActivity.class);
paliIntent.putExtra("val", strVal[2]);

Intent indonesiaIntent = new Intent(this, IndonesiaActivity.class);
indonesiaIntent.putExtra("val", strVal[3]);

TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("one").setIndicator("Pali").setContent(paliIntent));
tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Indonesia").setContent(indonesiaIntent));
}
}
  • 另一项 Activity :

package com.bsoft.activity;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import com.bsoft.shared.Shared;

public class IndonesiaActivity extends Activity {

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.indonesia);

String val = this.getIntent().getExtras().getString("val");

WebView indonesiaContent = (WebView) findViewById(R.id.wvIndoContent);
indonesiaContent.loadData("" + val.replace("\n", "<br/>") + "", Shared.MIME_TYPE, Shared.ENCODING);

Shared s = new Shared();
s.SetZoomControls(indonesiaContent, this);
}
}

关于java - Android 选项卡 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com