你好,我是android的初学者,我对动作栏溢出与片段组合有疑问。 我尝试制作的应用程序由三个独立的布局组成,我无法在这些布局上显示操作栏。
我设法创建了一个具有3个布局的应用程序,并且还使其他应用程序具有操作溢出的操作栏,但我不知道如何将这两个应用程序组合到一个应用程序中。 我正在寻找答案,但我没有找到一些可以解决我的问题。 这是我的第一个问题,所以我希望我已经设法描述了我需要解决的问题。 感谢所有试图帮助我解决问题的人。
这是我的app app代码(片段中的3个布局)
片段布局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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="second layout - layout B"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
片段的Java代码:
package com.example.mp.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
}
activity_main.xml代码:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"
xmlns:android="http://schemas.android.com/apk/res/android">
和MainActivity.java文件:
package com.example.mp.app;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import java.lang.reflect.Field;
public class MainActivity extends FragmentActivity {
ViewPager viewPager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
makeActionOverflowMenuShown();
}
private void makeActionOverflowMenuShown() {
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d("TAG", e.getLocalizedMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.clear();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
class MyAdapter extends FragmentPagerAdapter {
MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Log.d("MainActivity", "get Item is called " + i);
Fragment fragment = null;
if (i == 0){
fragment = new FragmentA();
Log.d("MainActivity", "get Item is called A " + i);
}
if (i == 1){
fragment = new FragmentB();
Log.d("MainActivity", "get Item is called B " + i);
}
if (i == 2){
fragment = new FragmentC();
Log.d("MainActivity", "get Item is called C " + i);
}
return fragment;
}
@Override
public int getCount() {
Log.d("MainActivity", "get Count is called");
return 3;
}
}
这是我用于动作溢出的操作栏的示例:
第1部分:https://developer.android.com/training/basics/actionbar/setting-up.html 第2部分:https://developer.android.com/training/basics/actionbar/adding-buttons.html
答案 0 :(得分:0)
默认情况下,您可以在Android中获取ActionBar覆盖,您需要将Activity类继承(扩展)到ActionBarActvity类。你也应该覆盖onCreateOptionsMenu方法。