Пользовательская вкладка в Android - получение ошибки при использовании "setIndicator" - PullRequest
3 голосов
/ 09 февраля 2011

Я пытаюсь создать пользовательскую вкладку в своем приложении для 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;
}

}

}

Кто-нибудь, пожалуйста, помогите мне решить эту проблему ...

спасибо заранее ..крис

Ответы [ 3 ]

3 голосов
/ 09 февраля 2011

К сожалению, метод setIndicator (View view) работает только для Android 1.6 и выше (версия 4).Если вы поддерживаете Android 1.5 (версия 3), вы можете использовать только String / CharSequence в качестве индикатора, используя setIndicator (метка CharSequence).

См. Ссылку: http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator

метод setIndicator (представление View) имеет вид «Так как: уровень API 4»

0 голосов
/ 03 сентября 2011

Я нашел этот урок очень хорошим:

http://kpbird.blogspot.com/2011/05/androidbottom-tabbar-control.html

0 голосов
/ 09 февраля 2011

Используйте следующий код для вкладок в 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...