電話: Samsung 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>
style.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>