android.support.v7.app.ActionBar& android.app.ActionBar

时间:2016-03-29 03:00:33

标签: android android-actionbar fragment tabview

我正在尝试使用标签创建应用,但我收到错误..请帮助..

当我使用'ActionBar actionBar'并在此行代码中出错时:
actionBar = getActionBar(); -> incompatible type
当我使用 android.app.ActionBar actionBar时,我在此行收到错误:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); -> error
        for (int i = 0; i < 3; i++) {
            actionBar.addTab(actionBar.newTab().setText(tabNames[i])
                    .setTabListener(this)); -> error
        }



app module :<br>
    android {
        compileSdkVersion 23
        buildToolsVersion "22.0.1"


        defaultConfig {
            useLibrary 'org.apache.http.legacy'
            minSdkVersion 21
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
}

tabActivity

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.os.Bundle;


public class tabActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter tabPagerAdapter;
    private android.app.ActionBar actionBar;
    private String[] tabNames = { "First", "Second"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabActivity);
        viewPager = (ViewPager) findViewById(R.id.pager);
        tabPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabPagerAdapter);
        actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 3; i++) {
            actionBar.addTab(actionBar.newTab().setText(tabNames[i])
                    .setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int postion) {
                actionBar.setSelectedNavigationItem(postion);
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
    }
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"


    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:requiresSmallestWidthDp="600"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Mn_Login"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TabActivity"
            android:label="TAB" />
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

而不是FragmentActivity尝试延长AppCompatActivity而不是getActionBar()使用getSupportActionBar()

答案 1 :(得分:0)

如果你想要Tab,你可以试试这个。

TabHost th = getTabHost();

        //Tab 1
        TabSpec tab1= th.newTabSpec("Tab Name");
        tab1.setIndicator("Frame Anim", getResources().getDrawable(R.drawable.ic));
        Intent move = new Intent(getApplicationContext(), FrameAnimation.class);
        tab1.setContent(move);
        //Tab 2
        TabSpec tab2= th.newTabSpec("Tab Name");
        tab2.setIndicator("Tween Amin", getResources().getDrawable(R.drawable.ic));
        Intent movetween = new Intent(getApplicationContext(), TweenAnimations.class);
        tab2.setContent(movetween); 
        th.addTab(tab1);
        th.addTab(tab2);

并在布局文件中添加TabHost。