Android 兼容多标签和姜饼

Android 兼容多标签和姜饼,android,android-2.3-gingerbread,Android,Android 2.3 Gingerbread,大家好,我已经为android开发了我的应用程序。。我使用了ICS操作栏: 如何使操作栏与姜饼兼容 主要活动: import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.app.AlertDialog; import android.content.Context; import android.content.Dialo

大家好,我已经为android开发了我的应用程序。。我使用了ICS操作栏:

如何使操作栏与姜饼兼容

主要活动:

    import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;

public class MainActivity extends ActionBarActivity  {

    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;
    public boolean StatusConnection = false;
    public int thread = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 


        TabAdapter = new TabPagerAdapter(getSupportFragmentManager()); 
        Tab = (ViewPager)findViewById(R.id.pager); 
        Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(
                                position);
                        Tab.setCurrentItem(position);
                    }

                });
        Tab.setAdapter(TabAdapter); 

        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(ActionBar.Tab arg0,FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onTabSelected(ActionBar.Tab arg0,FragmentTransaction arg1) {
                // TODO Auto-generated method stub
                Tab.setCurrentItem(arg0.getPosition());
            }
            @Override
            public void onTabUnselected(ActionBar.Tab arg0,FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
        }; 

        actionBar.addTab(actionBar.newTab().setText("a").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("b").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("c").setTabListener(tabListener));

        if (!isOnline()){
            try {

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Connessione Internet")
                .setMessage("Connessione Internet non disponibile.")
                .setCancelable(false)
                .setNegativeButton("Esci",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        finish();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
            catch(Exception e) { }
        }
    }

    private class TabPagerAdapter extends FragmentStatePagerAdapter {

        public TabPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new info();
                case 1:
                    return Arrivi.newInstance(thread, A_Compagnia, A_CodiceVolo, A_Citta, A_OraPrevista, A_OraStimata, A_StatoVolo); 
                case 2:
                    return Partenze.newInstance(thread, P_Compagnia, P_CodiceVolo, P_Citta, P_OraPrevista, P_OraStimata, P_StatoVolo); 

            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

谢谢大家

使用支持库中的操作栏版本。为此,您必须将appcompat_v7从支持库导入到您的工作空间,并在项目中引用它。之后,只需将getActionBar()更改为getSupportActionBar(),希望它能适用于较低版本。 您还必须扩展ActionBarActivity才能使其正常工作

谷歌教程没有提供代码来向您展示如何支持带有选项卡、viewpager和actionbar的姜饼设备。我在下面编辑了它来制作姜饼。主要变化在活动中,布局是为了方便而提供的。注意。需要添加Android支持库并将v7 appcompat作为参考库-请参阅

MyTwoListItemsActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyTwoListItemsActivity extends ActionBarActivity implements
    ActionBar.TabListener
{

  /**
   * The {@link android.support.v4.view.PagerAdapter} that will provide
   * fragments for each of the three primary sections of the app. We use a
   * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
   * keep every loaded fragment in memory. If this becomes too memory intensive,
   * it may be best to switch to a
   * {@link android.support.v4.app.FragmentStatePagerAdapter}.
   */
  AppSectionsPagerAdapter mAppSectionsPagerAdapter;

  /**
   * The {@link ViewPager} that will display the three primary sections of the
   * app, one at a time.
   */
  ViewPager mViewPager;

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections
    // of the app.
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
        getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();

    // Specify that the Home/Up button should not be enabled, since there is no
    // hierarchical
    // parent.
    actionBar.setHomeButtonEnabled(true);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for
    // when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager
        .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
        {
          @Override
          public void onPageSelected(int position)
          {
            // When swiping between different app sections, select the
            // corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a
            // reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
          }
        });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++)
    {
      // Create a tab with text corresponding to the page title defined by the
      // adapter.
      // Also specify this Activity object, which implements the TabListener
      // interface, as the listener for when this tab is selected.
      actionBar.addTab(actionBar.newTab()
          .setText(mAppSectionsPagerAdapter.getPageTitle(i))
          .setTabListener(this));
    }
  }

  @Override
  public void onTabReselected(Tab tab,
      android.support.v4.app.FragmentTransaction fragmentTransaction)
  {
  }

  @Override
  public void onTabSelected(Tab tab,
      android.support.v4.app.FragmentTransaction fragmentTransaction)
  {
    // When the given tab is selected, switch to the corresponding page in the
    // ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());

  }

  @Override
  public void onTabUnselected(Tab tab,
      android.support.v4.app.FragmentTransaction fragmentTransaction)
  {

  }

  /**
   * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one
   * of the primary sections of the app.
   */
  public static class AppSectionsPagerAdapter extends FragmentPagerAdapter
  {

    public AppSectionsPagerAdapter(FragmentManager fm)
    {
      super(fm);
    }

    @Override
    public Fragment getItem(int i)
    {
      switch (i)
      {
        case 0:
          // The first section of the app is the most interesting -- it offers
          // a launchpad into the other demonstrations in this example
          // application.
          return new LaunchpadSectionFragment();

        default:
          // The other sections of the app are dummy placeholders.
          Fragment fragment = new DummySectionFragment();
          Bundle args = new Bundle();
          args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
          fragment.setArguments(args);
          return fragment;
      }
    }

    @Override
    public int getCount()
    {
      return 2;
    }

    @Override
    public CharSequence getPageTitle(int position)
    {
      return "Section " + (position + 1);
    }
  }

  /**
   * A fragment that launches other parts of the demo application.
   */
  public static class LaunchpadSectionFragment extends Fragment
  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
      View rootView = inflater.inflate(R.layout.fragment_section_launchpad,
          container, false);

      // Demonstration of navigating to external activities.
      rootView.findViewById(R.id.demo_external_activity).setOnClickListener(
          new View.OnClickListener()
          {
            @Override
            public void onClick(View view)
            {
              // Create an intent that asks the user to pick a photo, but using
              // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching
              // the application from the device home screen does not return
              // to the external activity.
              Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
              externalActivityIntent.setType("image/*");
              externalActivityIntent
                  .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
              startActivity(externalActivityIntent);
            }
          });

      return rootView;
    }
  }

  /**
   * A dummy fragment representing a section of the app, but that simply
   * displays dummy text.
   */
  public static class DummySectionFragment extends Fragment
  {

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
      View rootView = inflater.inflate(R.layout.fragment_section_dummy,
          container, false);
      Bundle args = getArguments();
      ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(
          R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
      return rootView;
    }
  }

}
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.ActionBar.Tab;
导入android.support.v7.app.ActionBarActivity;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
公共类MyTwoListItemsActivity扩展了ActionBarActivity实现
ActionBar.TabListener
{
/**
*将提供的{@link android.support.v4.view.PagerAdapter}
*应用程序三个主要部分的每个部分的片段
*{@link android.support.v4.app.FragmentPagerAdapter}派生,它将
*将每个加载的片段都保存在内存中。如果内存太大,
*最好是换成一种新的方式
*{@link android.support.v4.app.FragmentStatePagerAdapter}。
*/
应用分区SPAGERAAdapter映射分区SPAGERAAdapter;
/**
*{@link ViewPager}将显示
*应用程序,一次一个。
*/
ViewPager mViewPager;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//主要部分
//应用程序的名称。
mAppSectionsPagerAdapter=新的AppSectionsPagerAdapter(
getSupportFragmentManager());
//设置操作栏。
最终ActionBar ActionBar=getSupportActionBar();
//指定不应启用主页/向上按钮,因为没有
//等级的
//家长。
actionBar.setHomeButtonEnabled(真);
//指定我们将在操作栏中显示选项卡。
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
//设置ViewPager,连接适配器并为其设置侦听器
//当
//用户在分区之间滑动。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener()
{
@凌驾
已选择页面上的公共无效(内部位置)
{
//在不同的应用程序分区之间滑动时,选择
//对应的选项卡。
//如果我们有一个
//参考
//标签。
actionBar.setSelectedNavigationItem(位置);
}
});
//对于应用程序中的每个部分,在操作栏中添加一个选项卡。
对于(int i=0;i<!--
  Copyright 2012 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!--
  Copyright 2012 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <Button android:id="@+id/demo_collection_button"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="Demo Collection"/>

        <Button android:id="@+id/demo_external_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Demo External Activity" />

    </LinearLayout>

</FrameLayout>
<!--
  Copyright 2012 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <Button android:id="@+id/demo_collection_button"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="Demo Collection"/>

        <Button android:id="@+id/demo_external_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Demo External Activity" />

    </LinearLayout>

</FrameLayout>
 <application
...
 android:theme="@style/Theme.AppCompat.Light" >