Android Studio中,让listView的每个Item居中
程序员文章站
2022-05-12 09:17:26
...
在你的Activity类中让Item居中的核心代码:
final ListView listViewPointResults = findViewById(R.id.listViewPointResults);
String[]ret = point.getResults();//这是我的字符串数组,将来要放到ListView中,也就是下面适配器的数据源
//
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(PointActivity.this,android.R.layout.simple_list_item_1, ret){
@Override
public View getView(int position, View convertView,ViewGroup parent) {//重载该方法,在这个方法中,将每个Item的Gravity设置为CENTER
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setGravity(Gravity.CENTER);
return textView;
}
};
listViewPointResults.setAdapter(arrayAdapter);//将适配器与ListView列表框关联