将操作栏添加到listactivity

时间:2013-08-30 11:55:13

标签: android actionbarsherlock listactivity

您好我创建了一个列表,我想添加操作栏。我对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 个答案:

答案 0 :(得分:14)

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

然后,在android:theme="@android:style/Theme.Holo.Light.DarkActionBar"课程的ListView_Activity下添加AndroidManifest.xml

示例

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

答案 1 :(得分:8)

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

在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>

答案 2 :(得分:6)

这是一个好方法:

在布局文件中: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);

祝你好运!

答案 3 :(得分:5)

然后在您的活动的onCreateOptionsMenu()方法中,将菜单资源膨胀到给定的菜单中,将每个项目添加到操作栏:

@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