2013-08-30 35 views
11

Xin chào vì vậy tôi đã tạo danh sách và tôi muốn thêm thanh tác vụ. Tôi khá mới cho android vì vậy tôi muốn biết làm thế nào để thêm thanh hành động trong khi sử dụng ListActivity. Bất kỳ trợ giúp sẽ được đánh giá cao. Cảm ơn Mã của tôi:Thêm thanh hành động vào danh sách hoạt động

 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); 
    } 

} 

Trả lời

5

Sau đó, trong onCreateOptionsMenu của hoạt động của bạn() phương pháp, thổi phồng tài nguyên đơn vào Menu cho thêm mỗi mục vào thanh hành động:

@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

+3

Tính năng này không hoạt động trên Android 4.4.2. Tôi đang sử dụng ListActivity với kiểu 'android: Theme.Holo.Light.DarkActionBar'. – Daniel

7

Bạn có thể sử dụng Chủ đề Holo, bạn chỉ cần trong màn hình này?

Trong manifest Android:

Đối với chỉ một màn hình, đặt atribute chủ đề như thế này:

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

Đối với tất cả màn hình, đặt atribute theme trong thẻ ứng dụng.

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

Bạn cũng có thể tạo chủ đề tùy chỉnh dựa trên Chủ đề ánh sáng Holo.

Ex:

android:theme="@style/My_Theme" > 

Trong styles.xml

<style name="My_Theme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style> 
12

First Hãy chắc chắn rằng tối thiểu Android của bạn API-14 or later.

Sau đó,, thêm android:theme="@android:style/Theme.Holo.Light.DarkActionBar" trong trong lớp AndroidManifest.xml của bạn.

Ví dụ

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

Đây là một cách tốt:

Trong file layout của bạn: 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> 

Bây giờ cho hoạt động của bạn:

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); 

Chúc may mắn!

+0

Tôi không nghĩ rằng myAdapter = new ListAdapter (getApplicationContext()); hoạt động vì bạn không thể khởi tạo lớp ListAdapter trừu tượng. –

+0

Trên thực tế ListAdapter là một lớp tùy chỉnh thực hiện BaseAdapter. Đây là chữ ký "lớp công khai ListAdapter mở rộng BaseAdapter" Vì vậy, bạn có thể tạo một đối tượng từ lớp ListAdapter và đối tượng này có các chức năng của BaseAdapter + các phương thức tùy chỉnh của bạn –

0

ListActivity chưa được chuyển đến AppCompat. Có lẽ vì bạn nên xem xét nó 'deprecated' và thay vào đó hãy sử dụng ListFragment.

Mảnh vỡ sẽ hoạt động với ActionBarActivity, chỉ cần đảm bảo chúng là những mảnh từ thư viện hỗ trợ.

Đọc qua liên kết này về các đoạn.

Đối với trường hợp sử dụng của bạn, tôi sẽ chỉ xác định đoạn trong xml.

Các vấn đề liên quan