安卓-想要把按钮放在ListView上方

4
我正在使用线性布局,其中包含一个按钮和一个列表视图。列表视图在运行时动态添加其项。因此,在此时,按钮会在列表视图后面,不可见,并且列表视图适配整个布局。 我想把按钮放在列表视图上方,并且加载列表视图后它也必须保持在上面。 谢谢您提前的帮助。
我的.xml文件是:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    android:background="#000000">


    <Button
        android:id="@+id/btn_searchNewDevices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:layout_gravity="left"
        android:background="#000000"
        android:textColor="#FFFFFF"
        android:text="Search New Devices"
      />   

     <ListView
        android:id="@+id/lst_add_newDevices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" >

    </ListView>
</LinearLayout>

在此输入图像描述

这里是搜索新设备的按钮。

点击该按钮,设备将搜索蓝牙并添加到列表视图中。 在此输入图像描述

这就是我的应用程序中发生的事情。

4个回答

1

我认为这个应该可以。试一下吧。

已编辑...

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn_searchNewDevices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginTop="5dip"

        android:text="Search New Devices"
        android:textColor="#000000" />

    <ListView
        android:id="@+id/lst_add_newDevices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btn_searchNewDevices" 
        android:scrollbars="horizontal">
    </ListView>

</RelativeLayout>

活动内容如下。

public class MainActivity extends Activity implements OnClickListener {
    ArrayAdapter<String> adapter;
    private Button mBTNSearch;
    private static ListView mLV_list;
    ArrayList<String> devicesList;
    SparseBooleanArray checked;
    RelativeLayout layout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mBTNSearch = (Button) findViewById(R.id.btn_searchNewDevices);
        devicesList = new ArrayList<String>();

        mLV_list = (ListView) findViewById(R.id.lst_add_newDevices);
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, devicesList);
        mBTNSearch.setOnClickListener(this);
      mLV_list.setAdapter(adapter);
    }

    public void onClick(View v) {

        adapter.add("new Device");
        adapter.notifyDataSetChanged();
    }

}

输出捕获如下:

enter image description here


给出其他解决方案,它会产生与我的相同的输出。 - yogesh kapuriya
如果可以,请发布您添加项目的方式。 - G_S

0
尝试在列表视图的高度中使用android:layout_weight="1",以确保您的按钮始终可见于屏幕上。
你应该尝试:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btn_searchNewDevices"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginTop="5dip"
        android:background="#000000"
        android:text="Search New Devices"
        android:textColor="#FFFFFF" />
    <ListView
        android:id="@+id/lst_add_newDevices"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="horizontal" >
    </ListView>
</LinearLayout>

或者您可以使用ListView的addHeaderView()方法。这里有一个tutorial


0

尝试使用这段代码来布局。

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       >
    </ListView>
</LinearLayout>


它输出的结果与我的相同。请提供其他解决方案。 - yogesh kapuriya

0
尝试将您的listview放置在scrollview内。

<Button
    android:id="@+id/btn_searchNewDevices"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_marginTop="5dip"
    android:background="#000000"
    android:text="Search New Devices"
    android:textColor="#FFFFFF" />

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="7dip"
    android:scrollbars="none" >

    <ListView
        android:id="@+id/lst_add_newDevices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" >
    </ListView>
</ScrollView>

可能会起作用。我还没有测试过,所以不确定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接