我有一個列表活動。我希望列表中的每個項目都包含一個文本視圖和三個圖像視圖。但是所有的項目都是一致的。如何正確配置我的xml和/或代碼?在xml的listActivity項目中設置一個cutom視圖
首先,只是爲了確保事情是工作,當我寫我的ListActivity我寫我的自定義ListAdapter有如下其只返回一個TextView一個getView功能:
public class TA extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class custom_adapter implements ListAdapter {
public int getCount() {
return 3;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
public View getView(int position, View convertView, ViewGroup parent){
TextView t2View = new TextView(getApplicationContext());
t2View.setText("some text");
return t2View;
}
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
}
this.setListAdapter(new custom_adapter());
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
}
這工作得很好。然後我試圖填補我的列表具有更復雜的視圖
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.tt_layout, (ViewGroup) findViewById(R.id.tt_root));
TextView tView = (TextView) layout.findViewById(R.id.tt);
switch(position){
case 0:tView.setText("Pos0");break;
case 1:tView.setText("Pos1");break;
case 2:tView.setText("Pos2");break;
case 3:tView.setText("Pos3");break;
}
return layout;
}
使用此XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tt_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#DAAA"
android:weightSum="1">
<TextView android:layout_height="wrap_content" android:id="@+id/tt" android:layout_width="wrap_content" android:text="Some text" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<ImageButton android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>
<ImageButton android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>
<ImageButton android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageButton>
</LinearLayout>
但
,而不是正確的行爲的所有項目的疊加彼此相鄰,而不是作爲單獨的列表項。
你的答案的作品,但這個問題涉及XML佈局的通貨膨脹不是在代碼 – tjb
最終佈局的建設,這似乎是儘管它沒有直接回答問題的最佳方式來實現這個問題的意圖,請參閱下面的unluddite在答案中給出的鏈接。 – tjb