你好我在listview后面调用了一个图像,listview是透明的。列表中的每条记录都出现在重复图像上。我怎么能修复这个重复的图像。这是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"
android:padding="7dp"
android:background="@drawable/ac"
android:cacheColorHint="#00000000"
>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="@+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>
由于
现在我已经更新了这样的xml文件 主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"
android:background="@drawable/ac"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView android:id="@+id/listView"
android:background="@drawable/ac"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
列出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"
android:cacheColorHint="#00000000"
>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="@+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>
以及以这种方式调用的活动
{ R.id.item_title, R.id.item_subtitle }
整个代码看起来像
public class Test extends ListActivity {
Prefs myprefs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
this.myprefs = new Prefs(getApplicationContext());
// install handler for processing gui update messages
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("...............");
try{
JSONArray earthquakes = json.getJSONArray("services");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", e.getString("taskid"));
map.put("pic", "Service name : " + e.getString("employeepic"));
map.put("serviceinfo", "" + e.getString("employeename")+ " : "+ e.getString("starttime")
+" To " + e.getString("endtime"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,
new String[] { "servicename", "serviceinfo" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:2)
我想您正在使用上面的XML来填充列表视图中的项目(即)getView()内部...
1.从上面的XML中删除android:background="@drawable/ac"
2.将其添加到ListView的XML说明,
<ListView android:id="@+id/listView"
android:background="@drawable/ac"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<强>解释强>
设置用于填充列表的布局的背景将生成'重复图像',因为您要为子项设置背景(即)listView的项目而不是列表。< / p>
修改强>
1.对于您的Activity,您将从布局文件(例如main.xml)加载ListView,在ListView的布局文件中添加背景。
2.从上面的代码中删除LinearLayout中的背景。
再次编辑:
使用以下XML,
的 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"
android:background="@drawable/ac"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView android:id="@+id/listView"
android:background="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
列出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"
android:cacheColorHint="#00000000"
android:background="#00000000"
>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="@+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>