在百分比佈局(其他佈局也行)中添加列表建議使用GridView
如果用listview的話過於繁瑣
public class MyNews extends Activity { private GridView gview; private List<Map<String, Object>> data_list; private SimpleAdapter sim_adapter; // 圖片封裝爲一個數組 private int[] icon = { R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher }; private String[] iconName = { "第一個", "第二個", "第三個", "第四個", "第五個", "第六個", "第七個", "第八個", "第九個", "第十個" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bai_fen); gview = (GridView) findViewById(R.id.bai_gv); //新建List data_list = new ArrayList<Map<String, Object>>(); //獲取數據 getData(); //新建適配器 String [] from ={"image","text"}; int [] to = {R.id.image,R.id.tvId}; sim_adapter = new SimpleAdapter(this, data_list, R.layout.listview_item, from, to); //配置適配器 gview.setAdapter(sim_adapter); } public List<Map<String, Object>> getData(){ //cion和iconName的長度是相同的,這裏任選其一都可以 for(int i=0;i<icon.length;i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("image", icon[i]); map.put("text", iconName[i]); data_list.add(map); } return data_list; }
<?xml version="1.0" encoding="utf-8"?> <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true"> <android.support.percent.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="top" android:background="#ffffff" app:layout_heightPercent="26%" app:layout_widthPercent="100%"> <android.support.percent.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="top" android:background="#ffffff" app:layout_heightPercent="50%" app:layout_widthPercent="100%"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="2016年信和大金融員工獎勵標準調整通知" android:textColor="#ff0000" android:textSize="15dp" /> <ImageView android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="left|top" android:src="@mipmap/tback" app:layout_heightPercent="100%" app:layout_widthPercent="13%" /> <ImageView android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="center|left" android:src="@mipmap/tlook" app:layout_heightPercent="50%" app:layout_widthPercent="13%" /> </android.support.percent.PercentFrameLayout> <android.support.percent.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="bottom" android:background="#f8f8f8" app:layout_heightPercent="50%" app:layout_widthPercent="100%"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left|center" android:paddingLeft="25dp" android:text="專項活動" android:textColor="#000000" android:textSize="25dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center" android:paddingRight="25dp" android:src="@mipmap/more" /> </android.support.percent.PercentFrameLayout> </android.support.percent.PercentFrameLayout> <android.support.percent.PercentFrameLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_gravity="top" android:background="#ffffff" app:layout_heightPercent="54%" app:layout_marginTopPercent="26%" app:layout_widthPercent="100%"> <GridView android:id="@+id/bai_gv" android:layout_width="wrap_content" android:layout_height="match_parent"> </GridView> </android.support.percent.PercentFrameLayout> </android.support.percent.PercentFrameLayout>