我正试图在我的android应用程序中创建一个自定义选项卡,我得到了一些示例代码,但该代码在setindicator方法中显示了错误…这是我的代码
我得到的错误是-类型setIndicator(CharSequence)
中的方法TabHost.TabSpec
不适用于参数(textview)
package com.myapp;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
//Custom Tabs
public class MyActivity extends Activity {
int tabHeight = 40;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
setContentView(main);
TabHost tabs = new TabHost(this);
tabs.setId(android.R.id.tabhost);
main.addView(tabs);
TabWidget tabWidget = new TabWidget(this);
tabWidget.setId(android.R.id.tabs);
tabs.addView(tabWidget);
FrameLayout tabContent = new FrameLayout(this);
tabContent.setId(android.R.id.tabcontent);
tabContent.setPadding(0, tabHeight, 0, 0);
tabs.addView(tabContent);
TextView content = new TextView(this);
content.setText("This is the Frame Content");
content.setId(100);
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec("Tab1");
tspec1.setIndicator(makeTabIndicator("One"));
tspec1.setContent(new PreExistingViewFactory(content));
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec("Tab2");
tspec2.setIndicator(makeTabIndicator("Two"));
tspec2.setContent(new PreExistingViewFactory(content));
tabs.addTab(tspec2);
TabSpec tspec3 = tabs.newTabSpec("Tab3");
tspec3.setIndicator(makeTabIndicator("Three"));
tspec3.setContent(new PreExistingViewFactory(content));
tabs.addTab(tspec3);
}
private TextView makeTabIndicator(String text){
TextView tabView = new TextView(this);
LayoutParams lp3 = new LayoutParams(LayoutParams.WRAP_CONTENT, tabHeight, 1);
lp3.setMargins(1, 0, 1, 0);
tabView.setLayoutParams(lp3);
tabView.setText(text);
tabView.setTextColor(Color.WHITE);
tabView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
tabView.setBackgroundDrawable( getResources().getDrawable(R.drawable.tab_indicator));
tabView.setPadding(13, 0, 13, 0);
return tabView;
}
class PreExistingViewFactory implements TabContentFactory{
private final View preExisting;
protected PreExistingViewFactory(View view){
preExisting = view;
}
public View createTabContent(String tag) {
return preExisting;
}
}
}
任何人请帮我解决这个问题…
提前……
克里斯
最佳答案
不幸的是,方法setindicator(视图视图)只适用于android 1.6及更高版本(版本4)。如果您支持android 1.5(版本3),则只能使用setindicator(charsequence标签)将string/charsequence用作指示符。
参见参考文献:http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator
注意,方法setIndicator(视图视图)是“因为:API级别4”