Android 使用下拉列表创建操作栏无法按预期工作
我正在创建一个带有多个下拉列表菜单的操作栏,但问题是它不工作,并且出现错误。有人能帮我吗 我从android官方网站上获得了教程,但我不明白它是如何与fragment和其他工具配合使用的 MainActivity.java main.xmlAndroid 使用下拉列表创建操作栏无法按预期工作,android,android-layout,drop-down-menu,android-fragments,android-actionbar,Android,Android Layout,Drop Down Menu,Android Fragments,Android Actionbar,我正在创建一个带有多个下拉列表菜单的操作栏,但问题是它不工作,并且出现错误。有人能帮我吗 我从android官方网站上获得了教程,但我不明白它是如何与fragment和其他工具配合使用的 MainActivity.java main.xml array.xml 水星 维纳斯 土 spiner\u下拉列表\u item.xml 您遇到了什么错误?您可以发布日志吗?错误在mainActivity.java代码中,它说:对于类型new ActionBar.OnNavigationListen
array.xml
水星
维纳斯
土
spiner\u下拉列表\u item.xml
您遇到了什么错误?您可以发布日志吗?错误在mainActivity.java代码中,它说:对于类型new ActionBar.OnNavigationListener(){},方法openFragmentTransaction()未定义。。。。。。它在方法replace下面加下划线,错误是:FragmentTraPromission类型中的方法replace(int,Fragment,String)不适用于参数(int MainActivity.ListContentFragment,String)
package com.devleb.droplitactionbar;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.drm.DrmStore.Action;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
public Spinner spiner;
ActionBar actionbar;
SpinnerAdapter mSpinnerAdapter;
OnNavigationListener mOnNavigationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list,
android.R.layout.simple_spinner_dropdown_item);
}
public void addSpinnerToActionbar( Spinner spiner)
{
actionbar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
mOnNavigationListener = new OnNavigationListener() {
// Get the same strings provided for the drop-down's ArrayAdapter
String[] strings = getResources().getStringArray(R.array.action_list);
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
// Create new fragment from our own Fragment class
ListContentFragment newFragment = new ListContentFragment();
FragmentTransaction ft = openFragmentTransaction();
// Replace whatever is in the fragment container with this fragment
// and give the fragment a tag name equal to the string at the position selected
ft.replace(R.id.fragment, newFragment, strings[position]);
// Apply changes
ft.commit();
return true;
}
};
}
public void addItemToSpinner()
{
spiner = (Spinner)findViewById(R.id.spinner);
List list = new ArrayList();
list.add("item1");
list.add("item2");
list.add("item3");
/*
ArrayAdapter dataAdapter = new ArrayAdapter(this, R.layout.spiner_drop_down_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spiner.setAdapter(dataAdapter);
*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ListContentFragment extends Fragment {
private String mText;
public void onAttach(Activity activity) {
// This is the first callback received; here we can set the text for
// the fragment as defined by the tag specified during the fragment transaction
super.onAttach(activity);
mText = getTag();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// This is called to define the layout for the fragment;
// we just create a TextView and set its text to be the fragment tag
TextView text = new TextView(getActivity());
text.setText(mText);
return text;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/fragment"
android:name="android.support.v4.app.ListFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="68dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="action_list">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
</string-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/action_list" />
</LinearLayout>