以Android小机器人作为ListView的背景图片,增加ListView的周边距离,分割线透明,效果如图所示:
主要是通过布局文件实现。给出代码实现activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher" >
<!--
最新版本的Android ListView可以不用设置这两项
android:cacheColorHint="#00000000"
android:scrollingCache="false"
-->
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:divider="#00000000"
android:dividerHeight="10dip"
android:fadingEdge="none"/>
</RelativeLayout>
测试的主Activity MainActivity.java:
package zhangphil.demo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(android.R.id.list);
String[] data = new String[100];
for (int i = 0; i < data.length; i++) {
data[i] = "数据" + i;
}
list.setAdapter(new MyArrayAdapter(this, android.R.layout.simple_list_item_1, data));
}
private class MyArrayAdapter extends ArrayAdapter<String> {
private String[] data;
private LayoutInflater layoutInflater;
private int resource;
public MyArrayAdapter(Context context, int resource, String[] data) {
super(context, resource);
this.data = data;
this.resource = resource;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = layoutInflater.inflate(resource, null);
TextView text = (TextView) convertView.findViewById(android.R.id.text1);
text.setText(getItem(position));
convertView.setBackgroundColor(0xff80deea);
// convertView.getBackground().setAlpha(128);
return convertView;
}
@Override
public String getItem(int pos) {
return data[pos];
}
@Override
public int getCount() {
return data.length;
}
}
}