【问题标题】:Adding actionbar to listactivity将操作栏添加到 listactivity
【发布时间】:2013-08-30 11:55:13
【问题描述】:

你好,所以我创建了一个列表,我想添加操作栏。我对 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);
    }

}

【问题讨论】:

    标签: android actionbarsherlock listactivity


    【解决方案1】:

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

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

    示例

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

    【讨论】:

    • 这对我有用,但我有一个菜单项会打开一个对话框窗口,由于主题没有从 appcompat 扩展,它不再工作,有没有办法解决这个问题?
    【解决方案2】:

    你可以使用 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>
    

    【讨论】:

      【解决方案3】:

      这是一个好方法:

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

      祝你好运!

      【讨论】:

      • 我不认为 myAdapter = new ListAdapter(getApplicationContext());有效,因为您无法实例化抽象 ListAdapter 类。
      • 其实ListAdapter是一个实现BaseAdapter的自定义类。这是签名“公共类 ListAdapter 扩展 BaseAdapter”因此您可以从 ListAdapter 类创建一个对象,该对象具有 BaseAdapter + 您的自定义方法的功能
      【解决方案4】:

      然后在您的活动的 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

      【讨论】:

      • 这不适用于我在 Android 4.4.2 上。我正在使用样式为 android:Theme.Holo.Light.DarkActionBar 的 ListActivity。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2016-05-28
      相关资源
      最近更新 更多