带ListView的Android布局

带ListView的Android布局,android,listview,button,alignment,Android,Listview,Button,Alignment,我真的对如下所示的布局感到失望: 我想有listview和下面,我需要有两个按钮并排,如上图所示(listview应该是可滚动的,按钮应该作为底部栏,即,在listview的顶部)。我试过使用上面的android:layout_和下面的android:layout_但都没用。有人能告诉我如何实现这个布局吗 这是我现在的布局代码,实际上我有这个代码,我可以实现这一点,但我不能点击这里的按钮 <?xml version="1.0" encoding="utf-8"?> <Rela

我真的对如下所示的布局感到失望:

我想有listview和下面,我需要有两个按钮并排,如上图所示(listview应该是可滚动的,按钮应该作为底部栏,即,在listview的顶部)。我试过使用上面的
android:layout_
和下面的
android:layout_
但都没用。有人能告诉我如何实现这个布局吗

这是我现在的布局代码,实际上我有这个代码,我可以实现这一点,但我不能点击这里的按钮

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/secondLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="15dp" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:id="@+id/firstLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLayout"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="#00ffffff"
    android:gravity="bottom"
    android:orientation="horizontal"
    android:padding="15dp" >

    <Button
        android:id="@+id/btnPrevious"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:background="@drawable/redbutton"
        android:onClick=""
        android:text="Previous"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:background="@drawable/redbutton"
        android:text="Next"
        android:textColor="#ffffff" />
</LinearLayout>

步骤1:去除无用的
线性布局
包装
列表视图
,将
android:padding
属性移动到
列表视图

步骤2:将
android:layout\u alignParentTop=“true”
android:layout\u over=“@+id/firstLayout
添加到
列表视图中

第三步:从剩余的
线性布局中去掉
android:layout_over=“@id/secondLayout”
,因为您肯定不希望它出现在
列表视图的上方

步骤4:将
按钮的
尺寸从
dp
切换到
sp
,以考虑字体缩放

步骤5:将
线性布局的宽度更改为
环绕内容
,或者删除
android:layout\u centerHorizontal=“true”
,因为无法将填充所有可用空间的内容居中放置


即兴,其余的应该可以,或者至少离你想要的要近得多。

把按钮添加到你的操作栏怎么样

否则,将ListView包装在中,并将您的RelativeLayout设置为LinearLayout

这可能适用于您:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/firstLayout" />

    <LinearLayout
        android:id="@+id/firstLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#00ffffff"
        android:orientation="horizontal"
        android:padding="15dp" >

        <Button
            android:id="@+id/btnPrevious"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:onClick=""
            android:text="Previous"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="Next"
            android:textColor="#ffffff" />
    </LinearLayout>

</RelativeLayout>

这很有效。您到底面临什么问题

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/secondLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="15dp" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
    android:id="@+id/firstLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLayout"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="#00ffff"
    android:gravity="bottom"
    android:orientation="horizontal"
    android:padding="15dp" >

    <Button
        android:id="@+id/btnPrevious"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:background="#FF0000"
        android:onClick="test"
        android:text="Previous"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:onClick="test"
        android:background="#FF0000"
        android:text="Next"
        android:textColor="#ffffff" />
</LinearLayout>


不要将
列表视图
放在
滚动视图
中。即使这样做有效,也不能解决OP的问题。它在设备上工作。甚至点击都有效。你使用的是哪一个android版本。我在4.4和2.3上进行了测试。@rachit-如果没有列表视图,使用简单的按钮我可以单击它们,但当我将listview添加到布局中时,从外部类加载时列表会使我的按钮可单击,并在4中进行测试。4@coder,不认识人。我无法重新生成问题。似乎正在我的设备上工作。单击“同时工作”:O:(