我想在我的android應用程序中創建一個自定義選項卡,並且我得到了一些示例代碼,但該代碼在setIndicator方法中顯示錯誤...這是我的代碼在android中的自定義選項卡 - 使用「setIndicator」時出現錯誤
錯誤我得到的 - 在類型TabHost.TabSpec
方法setIndicator(CharSequence)
是不適用的參數(的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;
}
}
}
有人請幫我解決這個問題...
提前感謝名單.. 克里斯
thanx user333,現在我檢查相同的代碼與更高的api水平和它wrking罰款...沒有錯誤沒有...它不工作與Android 1.5。什麼將是問題..? – Kris 2011-02-09 07:22:16
在哪一行(方法)你得到的錯誤在Android 1.5 – Pinki 2011-02-09 07:26:25