1

电话:三星 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>


样式.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>


片段.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>
4

1 回答 1

0

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

于 2013-11-06T12:43:19.220 回答