gpt4 book ai didi

android - 如何在 Android 的 ListView 中放置图像和文本

转载 作者:行者123 更新时间:2023-11-30 03:54:26 26 4
gpt4 key购买 nike

我是新来的,所以我有一个问题..我有这个菜单,专门是一个 Listview,我想在文本之前放一张图片..这是我的 menu2.java..

public class menu2 extends Activity {

private ListView listApps;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu2);

listApps = (ListView) findViewById(R.id.listView1);

String str[] = new String[] {"Messaging", "Phone Dialer", "Simple Math", "Splash", "Exit","Quit Application"};
//final ImageView imageView = (ImageView) findViewById(R.id.);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.activity_list_item,android.R.id.text1, str);

listApps.setAdapter(adapter);

listApps.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View aview, int position,
long id){
switch(position){
case 0:
//imageView.setImageResource(R.drawable.ic_launcher);
Intent i1 = new Intent(menu2.this,Messaging.class);
startActivity(i1);
break;
case 1:
Intent i2 = new Intent(menu2.this,PhoneDialer.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(menu2.this,SimpleMath.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(menu2.this,Splash.class);
startActivity(i4);
break;
case 4:
Intent i5 = new Intent(menu2.this,Exit.class);
startActivity(i5);
break;
case 5:

AlertDialog.Builder alt_bld = new AlertDialog.Builder(menu2.this);
alt_bld.setMessage("Are you sure you want to quit application?");
alt_bld.setCancelable(false);
alt_bld.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}

})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
alert.setTitle("Confirmation");
alert.setIcon(R.drawable.ic_launcher);
alert.show();

}
}
});
}

然后我有我的 menu2.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

我在左侧放置 ImageView 而在右侧放置 TextView 时遇到问题。

最佳答案

您是否了解如何使用 ListView 元素设置布局?您还必须为列表中的每一行创建一个单独的布局,例如 listview_row.xml 或您选择的任何名称。除此之外,您还必须创建一个自定义适配器,它将您的 textView 和 ImageView 绑定(bind)到每个 ListView 行。这使您可以更好地控制列表行。我已经写了一篇关于这个主题的博客文章。看一看,如果您需要更多帮助,请告诉我。祝你好运!

http://jadebyfield.blogspot.com/2012/10/custom-listview-with-textviews-and.html

关于android - 如何在 Android 的 ListView 中放置图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13575302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com