将按钮添加到列表活动 [英] Add buttons to a listactivity

查看:36
本文介绍了将按钮添加到列表活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListActivity 的布局.为了修改列表,我使用了菜单选项.但是要删除屏幕上的几次点击",我想在屏幕按钮中添加两个按钮,如果滚动列表,该按钮始终可见并且不受影响.

我的问题是我不知道如何添加这些按钮.我尝试了几种解决方案,但我管理的最好的解决方案是列表或按钮从布局中消失了.似乎我无法同时显示按钮和列表.

所以我的问题是如何创建一个可以同时拥有按钮和列表的布局?

提前致谢罗兰

解决方案

来自 http://developer.android.com/reference/android/app/ListActivity.html:

ListActivity 有一个默认布局,它由屏幕中央的单个全屏列表组成.但是,如果您愿意,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的视图布局来自定义屏幕布局.为此,您自己的视图必须包含一个 ID 为@android:id/list""的 ListView 对象

这里是一个例子:

ListActivity 可以这样创建:

<前>公共类 ListViewTest 扩展了 ListActivity {/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String[] values = {"一", "二", "三"};setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, values));setContentView(R.layout.main);}}

main.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"><列表视图android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_weight="1"android:id="@android:id/list"></ListView><按钮android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizo​​ntal"android:text="测试按钮"android:id="@+id/TestButton"></Button></LinearLayout>

I have a layout for a ListActivity. To modify the list I have used menu-options. But to remove a couple of "clicks" on the screen I'd like to add two buttons in the button of the screen that is always visible and not affected if the list is scrolled.

My problem is that I don't know how to add these buttons. I have tried a couple of solutions but the best I managed either the list or the buttons disapears from the layout. Seems that I can't get both buttons and list visible at the same time.

So my question is how to create a layout where I can have both buttons and the list?

Thanks in advance Roland

解决方案

From http://developer.android.com/reference/android/app/ListActivity.html:

"ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list""

EDIT: here is an example:

The ListActivity may be created like this:

public class ListViewTest extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] values = {"One", "Two", "Three"};

        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, values));

        setContentView(R.layout.main);
    }
}

The main.xml layout is as follows:

<?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">

    <ListView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:id="@android:id/list"></ListView>
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"
        android:text="Test button" 
        android:id="@+id/TestButton"></Button>
</LinearLayout>

这篇关于将按钮添加到列表活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆