0

4.1.2 で sherlock フラグメント アクティビティを使用して、sherlock テーマを実装しました。

使用して

android:theme="@style/Theme.Sherlock.Light"

次に、タブウィジェットが次のように表示されます(ここではタブアイコンが正常に表示されます)

ここに画像の説明を入力

これは問題ありませんが、同じコードが 2.3.3 で機能せず、次のようなタブが表示されます (ここでは、タブ アイコンが上記のように表示されません)。

ここに画像の説明を入力

すべてのバージョンでアイコン付きの同じタブウィジェットを取得する方法.?

これは私のxmlコードです

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:divider="@color/Columbia_Blue"
            android:baselineAligned="true"
            android:background="@android:color/darker_gray"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/white" />
    </LinearLayout>

</TabHost>

これはJavaコードです:

public class HomeFragment extends SherlockFragmentActivity {
    private TabHost mTabHost;
    private ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.home_tab_pager);
        BitmapDrawable bitmap = (BitmapDrawable) getResources().getDrawable(
                R.drawable.blue);
        bitmap.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        getSupportActionBar().setBackgroundDrawable(bitmap);

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mViewPager = (ViewPager) findViewById(R.id.pager);

        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

        mTabsAdapter.addTab(
                mTabHost.newTabSpec("List").setIndicator("",
                        getResources().getDrawable(R.drawable.list_icon)),
                ListFragmentActivity.ListFragment.class, null);

        mTabsAdapter.addTab(
                mTabHost.newTabSpec("Map").setIndicator("",
                        getResources().getDrawable(R.drawable.globe_icon)),
                ListFragmentActivity.ListFragment1.class, null);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    public static class TabsAdapter extends FragmentPagerAdapter implements
            TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost,
                ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
            notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        @Override
        public void onTabChanged(String tabId) {
            int position = mTabHost.getCurrentTab();
            mViewPager.setCurrentItem(position);

        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(),
                    info.args);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mTabs.size();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Used to put dark icons on light action bar

        menu.add("Pause").setIcon(R.drawable.pause_button)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add("Sign Out").setTitle("Sign out").setIcon(R.drawable.sign_out)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        menu.add("Settings").setTitle("Settings").setIcon(R.drawable.settings)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeFragment.this);

        if (item.getTitle().equals("Pause")) {
            builder.setTitle(R.string.alert);
            builder.setMessage(R.string.alert_pause_msg);
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            item.setTitle("Resume").setIcon(
                                    R.drawable.resume_button);

                        }
                    });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    });
            builder.show();

            return true;
        }
        if (item.getTitle().equals("Resume")) {
            item.setTitle("Pause").setIcon(R.drawable.pause_button);
            return true;
        }
        if (item.getTitle().equals("Sign out")) {
            AuthPreferences pref = new AuthPreferences(getApplicationContext());
            pref.clearCredentials();

            Intent intent = new Intent(this, FootMarkActivity.class);
            startActivity(intent);
            return true;
        }
        if (item.getTitle().equals("Settings")) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
        }
        return true;
    }
}
4

1 に答える 1