不是從ListActivity擴展,而是從一個簡單的Activity擴展你的類,並在該活動中放置一個ListView,你可以給邊距和填充ListView。只需使用已經使用ListView創建的同一個適配器即可。這是如何。 這是您的活動的佈局文件,稱它爲activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:marginTop="20sp"
android:marginBottom="10sp"
android:divider="@android:color/transparent"
android:id="@+id/listview"
android:padding="5sp"
android:dividerHeight="5.0sp"
></ListView>
</RelativeLayout>
現在在Java中創建一個活動。
public class MyListActivity extends Activity {
private ListView listView;
private MyAdapter myAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
this.listView = (ListView) findViewById(R.id.listview);
this.myAdapter = new MyAdapter(getApplicationContext());
//add whatever you want to add to your adapter
listView.setAdapter(myAdapter);
}
}
我希望這可以解決您的問題!
你希望整個事物的margin_top爲100dp?只需將其添加到容器視圖組。 – m02ph3u5
@ m02ph3u5雖然這裏沒有容器。這兩種佈局都在單獨的文件中,並且不會出現在任何地方的容器中。我誤解了嗎? –
不知道如果你可以用'ListActivity'來做到這一點,但嘗試獲取列表(它的id是'list')並以編程方式添加一個邊距。請參閱http://developer.android.com/reference/android/app/ListActivity.html – m02ph3u5