crv*_*crv 46 android header listactivity
目前我有一个扩展ListActivity类的类.我需要能够在列表上方添加一些始终可见的静态按钮.我试图从类中使用getListView()来获取ListView.然后我使用addHeaderView(View)将一个小布局添加到屏幕顶部.
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>
Run Code Online (Sandbox Code Playgroud)
在我设置适配器之前,我做了:
ListView lv = getListView();
lv.addHeaderView(findViewById(R.layout.header));
Run Code Online (Sandbox Code Playgroud)
除了从我的数据库中填充之外,这不会导致ListView发生任何事情.它上方没有按钮.
我尝试的另一种方法是将填充添加到ListView的顶部.当我这样做时它成功向下移动,但是,如果我在其上面添加任何内容,它会推送ListView.无论我做什么,似乎当我使用ListActivity时,我不能在ListView上面放几个按钮.
提前致谢.
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();
}
Run Code Online (Sandbox Code Playgroud)
活动的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>
Run Code Online (Sandbox Code Playgroud)
syn*_*nic 90
findViewById()
仅用于查找对象View的子视图.它不适用于布局ID.
您必须使用布局inflater将xml转换为它的相应View组件.像这样的东西:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
Run Code Online (Sandbox Code Playgroud)
我不确定为什么你的代码不只是抛出一个错误. findViewById()
可能刚刚返回null
,所以没有标题添加到您的列表中.
这是最简单的溶剂:
<?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>
Run Code Online (Sandbox Code Playgroud)
要么
<?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>
Run Code Online (Sandbox Code Playgroud)
而不是按钮,您可以添加另一个水平线性布局
经过一些研究,我能够弄清楚在ListActivity XML文档中混合使用TableLayout和LinearLayout,我能够在文档中添加标题.下面是我的XML文档,如果有人有兴趣看到它.虽然synic的方法可能是在他的解决方案工作一段时间之后的正确方法,但我无法按照我想要的方式使其运行.
AuditTab.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audittab);
getListView().setEmptyView(findViewById(R.id.empty));
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
73618 次 |
最近记录: |