这是可能的。而且不是太难。我以前做过同样的事情。首先,您将在项目包中创建一个 Java 类和 xml 文件。听到是一个例子,我认为Thana 是您的 java 类,activity_tha 是 xml 文件。
public class Thana extends Fragment implements OnItemClickListener{
// your global variable goes hear.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_tha, container, false);
// put you all code from old Activity class's onCreate() method .
// be careful this Fragmetn's onCreateView () returns a view object so look this
TextView headingThana = (TextView)rootView.findViewById(R.id.heading_thana);
// you always need a view reference to making any object of Widget from xml file.
return rootView;
}
// and other code goes hear as like as your old class
}
听到是另一个重点,您将无法从Context context; 或this 获取要使用的应用程序引用,因此您需要将它们全部替换为getActivity() 方法。
像这样
listView.setAdapter(new CustomListViewAdapter(this, thanaInfoList, thanaMobileNumber));
this 替换为getActivity();
listView.setAdapter(new CustomListViewAdapter(getActivity(), thanaInfoList, thanaMobileNumber));
对于菜单项,您需要 res/menu 文件夹中的 xml 文件。像这样
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/action_searchss"
android:title="action_searcheee"
android:showAsAction="always"
android:icon="@drawable/ic_launcher"
android:orderInCategory="1"
>
</item>
<item android:id="@+id/action_search"
android:title="action_search"
android:showAsAction="ifRoom"
android:icon="@drawable/ic_launcher"
android:orderInCategory="2"
>
</item>
现在在你的public boolean onCreateOptionsMenu(Menu menu) 方法中写下这个inflater.inflate(R.menu.activity_main_actions, menu);。 activity_main_actions 是您在菜单文件夹中新创建的 xml 文件............对于菜单选择,您可以覆盖此方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// your code goes hear may be you can use swith case . its better
}
这只是一个想法而不是确切的答案。谢谢你