2010-04-12 6 views
46

Derzeit habe ich eine Klasse, die die ListActivity-Klasse erweitert. Ich muss einige statische Buttons über der Liste hinzufügen können, die immer sichtbar sind. Ich habe versucht, die ListView mit getListView() innerhalb der Klasse zu greifen. Dann habe ich addHeaderView (View) verwendet, um ein kleines Layout oben auf dem Bildschirm hinzuzufügen.Android: Hinzufügen von statischen Kopfzeilen an den Anfang einer ListActivity

Header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <Button 
     android:id="@+id/testButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Income" 
     android:textSize="15dip" 
     android:layout_weight="1" /> 
</LinearLayout> 

Bevor ich den Adapter-Set ich tun:

ListView lv = getListView(); 
lv.addHeaderView(findViewById(R.layout.header)); 

Dies nichts führt zu dem Listview geschieht mit Ausnahme es aus meiner Datenbank gefüllt wird. Keine Schaltflächen erscheinen darüber.

Ein anderer Ansatz, den ich versucht habe, oben auf dem ListView Padding hinzuzufügen. Als ich das tat, ging es erfolgreich nach unten, aber wenn ich etwas darüber fügte, schob es die ListView um. Egal, was ich mache, es scheint, als ob ich nicht ein paar Knöpfe oberhalb der ListView platzieren könnte, wenn ich die ListActivity benutzte.

Vielen Dank im Voraus.

synic, habe ich Ihren Vorschlag zuvor versucht. Ich habe es nur aus Gründen der Vernunft versucht, und der Knopf wurde nicht angezeigt. Im Folgenden finden Sie die Layoutdatei für die Aktivität und den Code, den ich in oncreate() implementiert habe.

// Meine listactivity Ich versuche, den Header zu

public class AuditActivity extends ListActivity { 

    Budget budget; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     Cursor test; 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.audit); 
     ListView lv = getListView(); 
     LayoutInflater infalter = getLayoutInflater(); 
     ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false); 
     lv.addHeaderView(header); 
     budget = new Budget(this); 
     /* 
     try { 
      test = budget.getTransactions(); 
      showEvents(test); 
     } finally { 

     } 
     */ 
//  switchTabSpecial(); 
    } 

Layout.xml für Aktivität hinzufügen:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <ListView android:id="@android:id/list" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView android:id="@android:id/empty" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="@string/empty" /> 
</LinearLayout> 

Antwort

91

findViewById() nur Subviews des Objekts anzeigen finden arbeitet. Es funktioniert nicht mit einer Layout-ID.

Sie müssen Layout Inflater verwenden, um das XML in die entsprechenden View-Komponenten zu konvertieren. So etwas wie das:

ListView lv = getListView(); 
LayoutInflater inflater = getLayoutInflater(); 
View header = inflater.inflate(R.layout.header, lv, false); 
lv.addHeaderView(header, null, false); 

Ich bin mir nicht sicher, warum Ihr Code nicht nur einen Fehler warf. findViewById() wurde wahrscheinlich nur null zurückgegeben, und so wurde kein Header zu Ihrer Liste hinzugefügt.

+11

ich Sie zu einem Viewgroup müssen zuweisen und warf den aufgeblasenen Kopf nicht denken gibt. ViewGroup erweitert View, und die addHeaderView-Methode der ListView akzeptiert jedes View-Objekt. –

+1

@Glenn Bech iam stimme deiner Aussage zu –

+4

Perfekt, +1! Nur ein zusätzlicher Kommentar: Stellen Sie sicher, dass Sie den Listenadapter ** nach dem Hinzufügen der Header-Ansicht festlegen. Das Hinzufügen des Adapters vor dem Header gab mir eine Laufzeit-Ausnahme "Kann Header-Ansicht nicht zur Liste hinzufügen - setAdapter wie bereits genannt" – Addi

7

Nach einigen Recherchen konnte ich herausfinden, dass das Mischen von TableLayout und LinearLayout innerhalb meines ListActivity XML-Dokuments dem Dokument eine Kopfzeile hinzufügen konnte. Unten ist mein XML-Dokument, wenn jemand interessiert ist, es zu sehen. Während Synics Ansatz nach der Arbeit mit seiner Lösung wahrscheinlich der richtige Ansatz ist, konnte ich ihn nicht so funktionieren lassen, wie ich es wollte.

AuditTab.java

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.audittab); 
     getListView().setEmptyView(findViewById(R.id.empty)); 
} 

audittab.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    android:layout_width="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"> 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal"> 
     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <Button 
       android:id="@+id/btnFromDate" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="" 
       android:layout_weight="1" /> 
      <Button 
       android:id="@+id/btnToDate" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="" 
       android:layout_toRightOf="@+id/btnFromDate" 
       android:layout_weight="1" /> 
      <Button 
       android:id="@+id/btnQuery" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Query" 
       android:layout_toRightOf="@+id/btnToDate" 
       android:layout_weight="1" /> 

     </LinearLayout> 
    </TableRow> 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <ListView 
       android:id="@android:id/list" 
       android:layout_width="300dip" 
       android:layout_height="330dip" 
       android:scrollbars="none" /> 
      <TextView 
       android:id="@+id/empty" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingTop="10dip" 
       android:text="- Please select a date range and press query." /> 
     </LinearLayout> 
    </TableRow> 
</TableLayout> 

AuditItem.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:padding="10sp"> 
    <TextView 
     android:id="@+id/transactionDateLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Date: " /> 
    <TextView 
     android:id="@+id/transactionDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/transactionDateLabel" /> 
    <TextView 
     android:id="@+id/transactionTypeLabel" 
     android:layout_below="@id/transactionDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Type: " /> 
    <TextView 
     android:id="@+id/transactionType" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_below="@id/transactionDate" 
     android:layout_toRightOf="@id/transactionTypeLabel" /> 

    <TextView 
     android:id="@+id/transactionAmountLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:text="Amount: " 
     android:layout_below="@id/transactionDate" 
     android:layout_toRightOf="@id/transactionType" /> 
    <TextView 
     android:id="@+id/transactionAmount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/transactionDate" 
     android:layout_toRightOf="@id/transactionAmountLabel" /> 
    <TextView 
     android:id="@+id/transactionCategoryLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Category: " 
     android:layout_below="@id/transactionAmountLabel" /> 
    <TextView 
     android:id="@+id/transactionCategory" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/transactionAmountLabel" 
     android:layout_toRightOf="@id/transactionCategoryLabel" 
     /> 
    <TextView 
     android:id="@+id/transactionToAccountLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="To Account: " 
     android:layout_below="@id/transactionCategoryLabel" /> 
    <TextView 
     android:id="@+id/transactionToAccount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/transactionCategoryLabel" 
     android:layout_toRightOf="@id/transactionToAccountLabel" /> 
    <TextView 
     android:id="@+id/transactionFromAccountLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="From Account: " 
     android:layout_below="@id/transactionToAccountLabel" /> 
    <TextView 
     android:id="@+id/transactionFromAccount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/transactionToAccountLabel" 
     android:layout_toRightOf="@id/transactionFromAccountLabel" /> 
    <TextView 
     android:id="@+id/transactionNoteLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Note: " 
     android:layout_below="@id/transactionFromAccountLabel" /> 
    <TextView 
     android:id="@+id/transactionNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/transactionFromAccountLabel" 
     android:layout_toRightOf="@id/transactionNoteLabel" /> 
    <Button 
     android:id="@+id/editTransactionBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="40sp" 
     android:visibility="gone" 
     android:text="Edit" 
     android:layout_below="@id/transactionNoteLabel"/> 
    <Button 
     android:id="@+id/deleteTransactionBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="40sp" 
     android:text="Delete" 
     android:layout_below="@+id/transactionNoteLabel" 
     android:visibility="gone" 
     android:layout_toRightOf="@+id/editTransactionBtn" 
     android:ellipsize="end"/> 
</RelativeLayout> 
0

Hinzufügen ein statischer Header ist einfach, machen nur eine separate relative Ansicht, dass die hat alignParentTop (oder unten, rechts oder links) Attribut auf True festgelegt.

+1

Hier geht es nicht so sehr darum, wie man ein Layout mit einer Kopfzeile erstellt, hier geht es um ListActivities, die eigene Vorstellungen über das Layout hat. –

8

Hier ist die simpliest sollution:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/background"> 
<include layout="@layout/actionbar"/> 
    <ListView 
    android:id="@+id/tasklist_TaskListView" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    android:textColor="@color/baseFont"/> 
    <include layout="@layout/bottombar"/> 
</LinearLayout> 

oder

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/background"> 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
    <ListView 
    android:id="@+id/tasklist_TaskListView" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    android:textColor="@color/baseFont"/> 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 

statt Taste Sie eine weitere horizontale lineare Layout

+0

Können Sie bitte den Java-Code für diese Lösung bereitstellen? – lamba

+0

Es gibt nichts besonderes im Code. actionbar und bottombar sind nur ein weiteres XML-Layout, das in actionbar.xml und bottombar.xml definiert ist. – pcu

4

Die Listview-Antwort oben ist nützlich, aber scrollt mit der Liste hinzufügen und behält die Kopfgrafik nicht an der Spitze. Die beste Lösung, die ich gefunden habe, besteht darin, einen benutzerdefinierten Titel für die Aktivität festzulegen. Hier ist, was meine Konstruktor wie folgt aussieht:

public void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.your_listview_layout); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header); 
    ... 

Wo your_listview_layout.xml ein Listview konfiguriert und your_header.xml enthält unabhängig von benutzerdefinierten Header-Layout, das Sie mögen. Beachten Sie, dass die drei obigen Zeilen in genau dieser Reihenfolge aufgerufen werden müssen, um keine Laufzeitprobleme zu verursachen.

Das Tutorial, das mir half, war http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ und Sie viele ähnliche Seiten auf Stack-Überlauf bei der Suche nach dem Begriff „setFeatureInt“

Verwandte Themen