使用选项卡时,ActionBar不可见

时间:2013-10-14 13:38:15

标签: android tabs android-appcompat

电话:三星Galaxy S1
Android:2.3.3
android-support-library:v18
minSdkVersion:7

使用支持库(appcompat)向ActionBar添加标签时,ActionBar会消失 - >片段占据了洞屏空间。如果我没有向ActionBar添加选项卡,一切都很好(ActionBar可见)。总是要看ActionBar需要做些什么。

package test.appcompat;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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 Main extends ActionBarActivity implements ActionBar.TabListener {

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

        final ActionBar ab = getSupportActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ab.addTab(ab.newTab().setText("tab 1").setTabListener(this), 0);
        ab.addTab(ab.newTab().setText("tab 2").setTabListener(this), 1);
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) { }
    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Fragment f = new MyFragment();

        Bundle args = new Bundle();
        args.putString(MyFragment.ARG_TEXT, "Tab: "+ tab.getPosition());
        f.setArguments(args);

        getSupportFragmentManager().beginTransaction()
            .replace(android.R.id.content, f)
            .commit();
    }
}

class MyFragment extends Fragment {
    public static final String ARG_TEXT = "---";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment, container, false);
        ((TextView) v.findViewById(R.id.text))
            .setText(getArguments().getString(ARG_TEXT) + " hello");

        return v;
    }
}


清单(将DarkActionBar添加到活动中):          

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="test.appcompat.Main"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


styles.xml

<resources>
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat"> <!-- android:Theme.Light -->
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>


fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BAF8EC" >

    <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="32sp"
    android:textColor="#AAE720"
    android:text="Hello World"
    />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

更新到android-support-library v19解决了问题。