我是 Android 开发的新手,所以如果这是一个简单/新手的问题,以及任何不正确的术语,我深表歉意。
但我需要知道的是如何在其他 UI 元素(例如 TextView、ImageView 元素等)旁边包含一个列表
到目前为止,我所能实现的只是一个单独的列表 Activity ,为此我一直在使用 ListActivity 类类型。
我的列表 Activity :
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));
}
}
这是在我的主类/Activity 中启动的:
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()”的功能,这似乎只是切换到那个 Activity ,而不是将它“包含”到当前,这意味着“R.layout.main”中的任何元素(上面定义的“startActivity()) 的调用没有显示。
是否可以将此 Activity 包含在我的主要 Activity 中? 还是有更好的列单方式?
(我的目标最终是使列表数组动态化,只是认为 id 会影响任何建议的解决方案)。
感谢您的帮助(:
最佳答案
使用 startActivity 来启动您的 ListViewExample 会启动一个全新的 Activity (带有一个全新的 View )并将其放在堆栈的顶部。当您单击后退按钮时,将显示您的主要 Activity 。请看this链接以了解有关 Activity 生命周期的更多信息。
听起来您想要做的是在 ListView 旁边定义一些其他 UI 元素。我不知道您是否可以在 SIDE 上执行此操作,但我知道您可以在 ListView 的顶部或底部包含按钮/ TextView 。参见 this发布一个很好的例子,说明如何将按钮放在 ListView 下方。
编辑:举个例子(取自第二个链接),你会做这样的事情:
<?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 所说,您会希望您的主要 Activity 扩展 Activity。然后在您的 XML 中您将拥有以上内容。将 ListView 绑定(bind)到的方式是
ListView lv = (ListView)findViewById(R.id.list);
然后你可以绑定(bind)到它:
lv.setAdapter(...)
关于java - 如何将列表与其他 UI 元素一起包含在内? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6916758/