我应该使用什么语法(xml和java)来创建一个View,它在屏幕顶部有一些按钮(“Next”和“Back”),以及一个位于按钮下方的List? 我尝试了以下方法,但它根本不起作用:
main.xml中:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
<ListView android:id="@+id/ListView01" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
FirstScreen.java:
public class FirstScreen extends ListActivity {
/** Called when the activity is first created. */
String [] entries = {"one","two","three"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,entries);
ListView lv = new ListView(this,null,R.id.ListView01);
setListAdapter(adapter);
}
}
答案 0 :(得分:2)
这是一个应该有效的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Next"
android:layout_weight="1"
android:textSize="16dip"/>
<Button
android:id="@+id/back"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Back"/>
</LinearLayout>
<ListView
android:id="@+id/your_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
对于Java代码。
...
String[] entries = { "one", "two", "three" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, entries);
ListView lv = (ListView) findViewById(R.id.your_list);
lv.setAdapter(adapter);
...
我希望这会回答你的问题。
答案 1 :(得分:0)
如果您想使用ListActivity
,则需要将此用作列表视图IDandroid:id="@id/android:list"