В настоящее время у меня есть класс, который расширяет класс ListActivity. Я должен смочь добавить несколько статических кнопок выше списка, которые всегда видимы. Я попытался захватить ListView с помощью getListView () из класса. Затем я использовал addHeaderView (Представление) для добавления маленького расположения к вершине экрана.
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>
Прежде чем я установил адаптер, я делаю:
ListView lv = getListView();
lv.addHeaderView(findViewById(R.layout.header));
Это не приводит ни к чему происходящему с ListView за исключением него заполняемый от моей базы данных. Никакие кнопки не появляются выше его.
Другой подход я попробовал как добавляющий дополняющий к вершине ListView. Когда я сделал это, это успешно спустилось, однако, если я добавил кого-либо выше его, это протолкнуло ListView. Независимо от того, что я делаю кажется, как будто я не могу поместить несколько кнопок выше ListView, когда я использовал ListActivity.
Заранее спасибо.
synic, я попробовал Ваше предложение ранее. Я попробовал его снова только ради исправности, и кнопка не отображалась. Ниже файл расположения для действия и кода, который я реализовал в oncreate ().
//Мой listactivity я пытаюсь добавить заголовок к
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 для действия:
<?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>
findViewById()
работает только для поиска вложенных представлений объекта View. Она не будет работать с id макета.
Вам придется использовать layout inflater для преобразования xml в соответствующие компоненты View. Что-то вроде этого:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
Я не уверен, почему ваш код просто не выдает ошибку. findViewById()
, вероятно, просто возвращает null
, и поэтому никакой заголовок не добавляется в ваш список.
После некоторого исследования я смог выяснить, что смешивание TableLayout и LinearLayout в моем XML-документе ListActivity я смог чтобы добавить заголовок к документу. Ниже мой XML-документ, если кому-то интересно. Хотя подход Synic, вероятно, является правильным, после работы с его решением какое-то время мне не удавалось заставить его работать так, как я хотел.
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>