47 votes

Android: Ajout d'un en-tête statique en haut d'une ListActivity

Actuellement, j'ai une classe qui étend la ListActivity classe. J'ai besoin d'être en mesure d'ajouter un peu de statique des boutons au-dessus de la liste qui sont toujours visibles. J'ai tenté de saisir la liste à l'aide de getListView() dans la classe. Ensuite, j'ai utilisé addHeaderView(View) pour ajouter une petite mise en page vers le haut de l'écran.

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>

Avant de mettre l'adaptateur que j'ai à faire:

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

Il en résulte ne se passe rien à la ListView sauf pour elle d'être remplie à partir de ma base de données. Pas de boutons apparaissent au-dessus d'elle.

Une autre approche, j'ai essayé que l'ajout d'un rembourrage en haut de la liste. Quand j'ai fait ce qu'il a réussi, cependant, si j'ai ajouté tout au-dessus d'elle, il a poussé la ListView. Peu importe ce que je fais, il me semble que si je ne peux pas mettre un peu de boutons au-dessus de la liste lorsque j'ai utilisé le ListActivity.

Merci à l'avance.

synic, j'ai essayé ta suggestion précédemment. J'ai essayé à nouveau, juste pour le plaisir de la santé mentale, et le bouton ne s'affiche pas. Ci-dessous le fichier de mise en page pour l'activité et le code que j'ai mis en œuvre dans le oncreate().

//Mon listactivity je suis en train d'ajouter l'en-tête de

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 pour l'activité:

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

90voto

synic Points 12644

findViewById() ne fonctionne que pour trouver des sous-vues de l'objet View. Cela ne fonctionnera pas sur un identifiant de mise en page.

Vous devrez utiliser layout inflater pour convertir le fichier XML en ses composants View correspondants. Quelque chose comme ça:

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

Je ne sais pas pourquoi votre code n'a pas simplement généré une erreur. findViewById() était probablement en train de renvoyer null et aucun en-tête n'a donc été ajouté à votre liste.

9voto

pcu Points 130

Voici la solution la plus simple:

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

ou

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

au lieu de bouton, vous pouvez ajouter une autre mise en page linéaire horizontale

7voto

crv Points 1813

Après quelques recherches, j'ai été capable de comprendre que le mélange TableLayout et LinearLayout dans mon ListActivity document XML j'ai pu ajouter un en-tête du document. Ci-dessous mon document XML si quelqu'un est intéressé pour le voir. Alors que synic approche est sans doute la bonne approche, après le travail, avec sa solution pendant un certain temps, j'ai été incapable de le faire fonctionner comme je le voulais.

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>

4voto

Melinda Green Points 1033

La liste de la réponse ci-dessus est utile, mais des rouleaux de papier avec la liste et permet de ne pas garder les graphiques d'en-tête en haut. La meilleure solution que j'ai trouvé est de mettre un titre personnalisé à l'activité. Voici ce que mon constructeur ressemble:

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

Où your_listview_layout.xml configure une ListView, et your_header.xml contient tout ce que l'en-tête personnalisé de mise en page que vous le souhaitez. Il suffit de noter que les trois lignes ci-dessus doit être appelée exactement dans l'ordre pour ne pas causer des problèmes d'exécution.

Le tutoriel qui m'a aidé a http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ et vous pouvez trouver de nombreuses pages connexes sur un Débordement de Pile par la recherche pour le terme "setFeatureInt"

0voto

rsfrost Points 31

L'ajout d'un en-tête statique est simple. Créez simplement une vue relative distincte dont l'attribut alignParentTop (ou bottom, right ou left) est défini sur true.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X