简体   繁体   English

使用选项卡时不显示ActionBar

[英]ActionBar not visible when using tabs

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

When adding tabs to the ActionBar using the support library (appcompat) the ActionBar disappears -> the fragment takes the hole screen space. 使用支持库(appcompat)将选项卡添加到ActionBar时,ActionBar消失->该片段占用了孔屏幕空间。 If I don't add tabs to the ActionBar, everything is all right (the ActionBar is visible). 如果我不将选项卡添加到ActionBar,则一切正常(ActionBar可见)。 What needs to be done to always see the 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;
    }
}


Manifest (added DarkActionBar to the activity): 清单 (向活动添加了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 : 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 : 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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM