android 中的自定义选项卡 - 使用“setIndicator”时出现错误

发布于 2024-10-16 07:02:25 字数 2740 浏览 1 评论 0原文

我正在尝试在我的 Android 应用程序中创建一个自定义选项卡,并且我得到了一些示例代码,但该代码在 setIndicator 方法中显示错误...这是我

收到的代码错误是 - 方法 setIndicator(CharSequence)< TabHost.TabSpec 类型中的 /code> 不适用于参数 (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;
}

}

}

任何人请帮助我解决这个问题...

提前感谢.. 克里斯

i am trying to create a custom tab in my android app and i got some sample codes but that code is showing errors in setIndicator method...here is my code

error i am getting is - The method setIndicator(CharSequence) in the type TabHost.TabSpec is not applicable for the arguments (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;
}

}

}

anybody pls help me to fix this issue ...

thanx in advance ..
kris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放手` 2024-10-23 07:02:25

不幸的是,方法 setIndicator(View view) 仅适用于 Android 1.6 及更高版本(版本 4)。如果您支持 Android 1.5(版本 3),则只能使用 setIndicator(CharSequence label) 将 String/CharSequence 用作指示器。

请参阅参考: http://developer.android.com/reference /android/widget/TabHost.TabSpec.html#setIndicator

请注意,方法 setIndicator(View view) 是“Since: API Level 4”

Unfortunately the method setIndicator(View view) only works for Android 1.6 and up (version 4). If you are supporting Android 1.5 (version 3) you can only use a String/CharSequence as an indicator using the setIndicator(CharSequence label).

See the reference: http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator

Notice that the method setIndicator(View view) is "Since: API Level 4"

千年*琉璃梦 2024-10-23 07:02:25

中对选项卡使用以下代码

在 android Java 文件

public class Main extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main1);

        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the Movies tab.
        intent = new Intent().setClass(this, BarActivity.class);
        // Initialise a TabSpec for the Movies tab and add it to the TabHost.
        spec = tabHost.newTabSpec("Nights").setIndicator("Nights").setContent(intent);
        tabHost.addTab(spec);

        // Do the same things for the Theatres tab.
        intent = new Intent().setClass(this, BarActivity.class);
        spec = tabHost.newTabSpec("Weeks").setIndicator("Weeks").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, BarActivity.class);
        spec = tabHost.newTabSpec("Months").setIndicator("Months").setContent(intent);
        tabHost.addTab(spec);
        getTabHost().getTabWidget().getChildAt(0).setLayoutParams(new 
                LinearLayout.LayoutParams(100,50)); 
        getTabHost().getTabWidget().getChildAt(1).setLayoutParams(new 
                LinearLayout.LayoutParams(100,50)); 
        getTabHost().getTabWidget().getChildAt(2).setLayoutParams(new 
                LinearLayout.LayoutParams(100,50)); 
        tabHost.setCurrentTab(0);
    }
}

和 main1.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
    </FrameLayout>
</LinearLayout>
</TabHost>

Use the following code for tabs in android

Java File

public class Main extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main1);

        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Reusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the Movies tab.
        intent = new Intent().setClass(this, BarActivity.class);
        // Initialise a TabSpec for the Movies tab and add it to the TabHost.
        spec = tabHost.newTabSpec("Nights").setIndicator("Nights").setContent(intent);
        tabHost.addTab(spec);

        // Do the same things for the Theatres tab.
        intent = new Intent().setClass(this, BarActivity.class);
        spec = tabHost.newTabSpec("Weeks").setIndicator("Weeks").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, BarActivity.class);
        spec = tabHost.newTabSpec("Months").setIndicator("Months").setContent(intent);
        tabHost.addTab(spec);
        getTabHost().getTabWidget().getChildAt(0).setLayoutParams(new 
                LinearLayout.LayoutParams(100,50)); 
        getTabHost().getTabWidget().getChildAt(1).setLayoutParams(new 
                LinearLayout.LayoutParams(100,50)); 
        getTabHost().getTabWidget().getChildAt(2).setLayoutParams(new 
                LinearLayout.LayoutParams(100,50)); 
        tabHost.setCurrentTab(0);
    }
}

and main1.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
    </FrameLayout>
</LinearLayout>
</TabHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文