2011-08-22 68 views
1

对不起,我是一个新手:(Android的添加按钮前的ListView

如何在ListView顶部添加Button和风格喜欢这里: This Image

+1

可以参考这个[link](http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/) –

+0

[This](https:/ /android.googlesource.com/platform/packages/apps/DeskClock/+/gingerbread-release)是姜饼原始闹钟应用程序的源代码。您可以看到使用的按钮样式不是标准样式。 – danieleds

回答

2

我认为i'ts更好,当你第一次使用的LinearLayout与垂直方向,并把一个按钮,并把一个ListView。在的LinearLayout将您的 “内容包装” 是这样的: main.xml中

<?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"> 
    <Button 
     android:text="Button" 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:entries="@array/list"/>  
</LinearLayout> 

和字符串数组项:

<string-array name="list"> 
    <item>Entry1</item> 
    <item>Entry2</item> 
    <item>Entry3</item> 
    <item>Entry4</item> 
</string-array> 
0

首先,我只是想确认,是Button始终,即使ListView滚动的ListView顶部,该Button仍然存在或Button滚动过也

如果是第一种情况,这是很简单的,J乌斯特垂直LinearLayoutButton之前ListView

如果是第二种情况,它是相当复杂的。因为你不应该在ScrollView里面使用ListView,所以我建议你让ListView的第一行成为一个内部有Button的自定义视图。为了做到这一点,请查看如何扩展BaseAdapterLayoutInflate

0

也许这个按钮不是列表视图的一部分。 尝试拨打布局是这样的:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</LinearLayout> 
+0

'ListView'饿了高度,并会填充'LinearLayout'中所有可用的高度。 –

0

可以使用RelativeLayout的做到这一点:

<RelativeLayout android:id="@+id/parentLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <Button android:id="@+id/btn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="My Button" 
     android:alignParentTop = "true"/> 

    <ListView android:id="@+id/myList" 
    android:layout_below="@id/btn" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

</RelativeLayout> 
0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_gravity="top|center_horizontal" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:text="Add Alarm"/> 
    <ListView android:layout_width="fill_parent" android:layout_height="0dip" 
     android:layout_weight="1.0"></ListView> 
</LinearLayout> 

这应该给出所需的布局。

+1

这项工作。但风格不像这个形象 – Rose

0

您可以设计自己的布局中正在适配器中充气的按钮,并将按钮可见性消除。在适配器中,如果psoition为0,并且如果为0,则只能在getview方法中检查按钮,否则使按钮变为可见。这样,您在列表顶部有一个按钮,并且可以滚动。要使按钮可点击,只需在getview方法中实现onclick方法。它非常简单。