我有一个 2 列的行布局,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textSize="13sp">
<TextView android:id="@+id/TRAIN_CELL"
android:layout_width="275dip"
android:layout_height="wrap_content"/>
<ImageView android:id="@+id/TO_CELL"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/arrow_button"/>
</LinearLayout>
一旦我从数据库中获取项目,我就会将它们显示在左列中,在右列中我想要一个带有箭头的小图像,表示它是一个可点击的项目。但我不知道如何使图像渲染。以下是我目前填充该列表的方式:
我有这两个变量:
private List<HashMap<String, String>> fillMaps;
private SimpleAdapter adapter;
一开始我是这样设置的:
list = (ListView) findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//HashMap<String, String> map = new HashMap<String, String>();
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, fillMaps, R.layout.questions_list,
new String[] {"train", "to"},
new int[] {R.id.TRAIN_CELL, R.id.TO_CELL});
// This was the middle item R.id.FROM_CELL,
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
当数据从服务器返回时,我会像这样填充列表:
if ( obj != null )
{
questions.clear();
for ( int i = 0; i < obj.length(); i++ )
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject o = obj.getJSONObject(i);
question_id = o.getString("question_id");
question = o.getString("question");
question_by_member_id = o.getString("member_id");
Question q = new Question ( );
q.setQuestion( question );
q.setQuestionId( question_id );
q.setQuestionByMemberId(question_by_member_id);
map.put("train", question);
//map.put("from", ">");
//map.put("to", ">");
fillMaps.add(map);
questions.add( q );
}
adapter.notifyDataSetChanged();
}
但我无法渲染图像。我认为问题的一部分是我使用字符串作为列表所需的数据,但我不确定如何用图像处理这个问题。顺便说一句,图像始终是相同的图像。
谢谢!! :)
最佳答案
这是你想要的吗?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textSize="13sp">
<TextView android:id="@+id/TRAIN_CELL"
android:layout_width="275dip"
android:layout_height="wrap_content"/>
<ImageView android:id="@+id/TO_CELL"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/arrow_button"/>
</LinearLayout>
添加
(抱歉,有时我会错过通知,因为它们整夜堆积起来。我相信您应该能够在 Stack Overflow 上一一或一次全部阅读消息/删除通知,就像收件箱一样......)
无论如何,如果TO_CELL
的图像始终相同,只需仅在 XML 中设置图像即可。将您的 SimpleAdapter 更改为:
adapter = new SimpleAdapter(this, fillMaps, R.layout.questions_list,
new String[] {"train"},
new int[] {R.id.TRAIN_CELL});
现在您的适配器仅处理一个字符串,您可以将其简化为 ArrayAdapter(如果您愿意)。
简化示例
您还可以显示一个 TextView 并显示一个小“下一个”箭头,如下所示:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_media_play"
android:padding="10sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
(LinearLayout 和单独的 ImageView 并不是绝对必要的。我将其保存为 list_item.xml
。)
这是一个简单的预览,请理解,我留下了很多非关键行,以免重复:
public class Example extends Activity {
ArrayAdapter<String> adapter;
List<String> list = new ArrayList<String>();
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, list);
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
updateList();
}
public void updateList() {
...
if ( obj != null ) {
questions.clear();
list.clear();
for ( int i = 0; i < obj.length(); i++ ) {
JSONObject o = obj.getJSONObject(i);
...
question = o.getString("question");
list.add(question);
}
adapter.notifyDataSetChanged();
}
}
}
希望这有帮助!
关于Android - 如何将图像放置在 2 列列表的其中一列中并使图像垂直居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12307593/