【问题标题】:how to include list along side other UI elements? (Android)如何在其他 UI 元素旁边包含列表? (安卓)
【发布时间】:2011-10-18 12:11:14
【问题描述】:

我是 android 开发的新手,所以如果这是一个简单/菜鸟式的问题,以及任何不正确的术语,我深表歉意。

但我需要知道的是如何在其他 UI 元素(如 TextView、ImageView 元素等)旁边包含一个列表

到目前为止,我所能实现的只是一个单独的列表活动,为此我一直使用 ListActivity 类类型。

我的列表活动:

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ListViewExample extends ListActivity 
{
    String[] exampleList = {
            "Item 1",
            "Item 2",
            "Item 3"
            //etc etc
    };

    @Override  
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.listview);

        setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, exampleList));
    }
}

在我的主要课程/活动中开始:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class NewtestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startActivity(new Intent( this, ListViewExample.class));
    }
}

但是使用“startActivity()”的功能,这似乎只是切换到该活动,而不是将其“包含”到当前,这意味着“R.layout.main”中的任何元素(上面定义的“startActivity()) 调用没有显示出来。

无论如何要将此活动包含在我的主要活动中吗? 或者有没有更好的制作清单的方法?

(我的目标最终是使列表数组动态化,只是认为 id 会影响任何建议的解决方案)。

感谢您的帮助(:

【问题讨论】:

    标签: java android xml android-layout listactivity


    【解决方案1】:

    使用 startActivity 启动您的 ListViewExample 会启动一个全新的活动(带有全新的视图)并将其放在堆栈顶部。当您单击后退按钮时,将显示您的主要活动。请参阅this 链接以了解有关活动生命周期的更多信息。

    听起来您想要在列表视图旁边定义一些其他 UI 元素。我不知道您是否可以在 SIDE 上执行此操作,但我知道您可以在列表视图的顶部或底部包含按钮/文本视图。请参阅 this 帖子作为如何在列表视图下方放置按钮的一个很好的示例。

    编辑:作为一个例子(取自第二个链接),你会做这样的事情:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/testbutton"
            android:text="@string/hello" android:layout_alignParentBottom="true" />
    
        <ListView android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:id="@+id/list"
            android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />
    
    </RelativeLayout>
    

    现在您也可以根据需要将该按钮放在底部,或者在顶部添加一个文本框,在底部添加一个按钮。

    是的,正如 Espiandev 所说,您希望您的主要活动扩展活动。然后在您的 XML 中,您将拥有上述内容。您将列表视图绑定到的方式是

    ListView lv = (ListView)findViewById(R.id.list);

    然后你可以绑定到它:

    lv.setAdapter(...)

    【讨论】:

    • 嗨,感谢您的建议(:第一个解决方案有效(:但至于第二个,我得到一个强制关闭 D:关于为什么的任何想法?再次感谢(:
    • 可能是因为您正在调用 setContentView(R.layout.listview) ?您应该将 contentview 设置为主要的 xml 文件名。就像使用 main.xml 一样,使用 R.layout.main。
    • 在复制和粘贴你的和下面的编辑后现在可以工作了,所以我一定是在某个地方有一个小错字。再次感谢您的时间(:
    【解决方案2】:

    另一个可能更具可扩展性的答案的替代方法是简单地使ListViewExample 扩展一个基本的Activity。然后,在onCreate() 方法中,使用findViewById() 检索ListView,然后在此使用setAdapter()。例如,如果您的 ListView 被赋予 id listview1:

    public class ListViewExample extends Activity {
        String[] exampleList = {
                "Item 1",
                "Item 2",
                "Item 3"
                //etc etc
        };
    
        @Override  
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.listview);
    
            // Get an instance of your listview in code
            ListView listview = (ListView) findViewById(R.id.listview1);
    
            // Set the listview's adapter
            listview.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, exampleList));
        }
    }
    

    这将使您可以灵活地进行布局,而不仅仅是一个列表视图

    【讨论】:

    • 我认为您的意思是 ListViewExample 扩展 Activity 而不是 ListActivity
    • 这一行是“ListView listview = (ListView) findViewById(R.id.listview1);”参考 R.layout.listview 中列表的布局/样式?如果是这样,在更改 id (listview1) 以匹配我正在使用的 ID 后,我遇到了强制关闭错误。关于为什么的任何想法?谢谢(:
    • 啊,它现在可以工作了,我直接复制了代码就可以了,所以我一定是在某个地方出现了一个小错误,再次感谢(:
    • 是的,R.id.listview1 指的是列表视图本身的 id,而不是布局。只需在布局中放置一个 listview 并将其指定为 id listview1,就可以完美运行。如果这是您接受的答案,请在它旁边打勾:)
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2020-03-27
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多