11

你好,所以我创建了一个列表,我想添加操作栏。我对 android 很陌生,所以我想知道如何在使用 ListActivity 时添加操作栏。任何帮助将不胜感激。谢谢我的代码:

     public class MainActivity extends ListActivity {

     ArrayList<Item> items = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        items.add(new SectionItem("2x2 Matrices"));
        items.add(new EntryItem("Adding 2 Matrices"));
        items.add(new EntryItem("Subtracting 2 Matrices"));
        items.add(new EntryItem("Multiplying 2 Matrices"));
        items.add(new EntryItem("Multiplying by a constant"));
        items.add(new EntryItem("Dividing 2 Matrices"));
        items.add(new EntryItem("Negative of a Matrix"));
        items.add(new EntryItem("Inverse of a Matrix"));
        items.add(new EntryItem("Determinant of a Matrix"));

        /*Section2*/
        items.add(new SectionItem("3x3 Matrices"));
        items.add(new EntryItem("Item 4"));
        items.add(new EntryItem("Item 5"));
        items.add(new EntryItem("Item 6"));
        items.add(new EntryItem("Item 7"));
        /*Section3*/
        items.add(new SectionItem("Category 3"));
        items.add(new EntryItem("Item 8"));
        items.add(new EntryItem("Item 9"));
        items.add(new EntryItem("Item 10"));
        items.add(new EntryItem("Item 11"));
        items.add(new EntryItem("Item 12"));

        EntryAdapter adapter = new EntryAdapter(this, items);

        setListAdapter(adapter);
    }

}
4

4 回答 4

14

首先 - 确保您的 Android 最低API-14 or later.

然后android:theme="@android:style/Theme.Holo.Light.DarkActionBar",在你ListView_ActivityAndroidManifest.xml课堂下添加 。

例子

        <activity android:name=".Your_ListView_Activity"
                  android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
                  android:label="ListView_Activity_Label">
于 2015-04-19T14:23:21.697 回答
9

你可以使用 Holo Themes,你只需要在这个屏幕上?

在 Android 清单中:

只有一个屏幕,放置属性主题,如下所示:

<activity
android:theme="@android:style/Theme.Holo.Light.DarkActionBar">
</activity>

对于所有屏幕,将属性主题放在应用程序标签中。

<application
android:theme="@style/My_Theme" >

您还可以根据 Holo Light Theme 制作自定义主题。

前任:

android:theme="@style/My_Theme" >

在styles.xml

<style name="My_Theme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
于 2013-08-30T12:19:14.250 回答
7

这是一个好方法:

在您的布局文件中:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
</ListView>

现在为您的活动:

public class MainActivity extends ActionBarActivity
{
    private ListView listView;
    private ListAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        myAdapter = new ListAdapter(getApplicationContext());
        listView.setAdapter(myAdapter);

祝你好运!

于 2015-07-02T21:17:58.603 回答
5

Then in your activity's onCreateOptionsMenu() method, inflate the menu resource into the given Menu to add each item to the action bar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

More info for action bar

于 2013-08-30T12:15:22.437 回答