【问题标题】:AlertDialog with images in listview列表视图中带有图像的 AlertDialog
【发布时间】:2014-04-01 08:13:14
【问题描述】:

是否可以使用 AlertDialog 在文本旁边显示带有图像的列表视图? 或者我是否必须使用列表视图(例如)实现 DialogFragment?

编辑: 是否也可以使用带有 .setMultiChoiceItems 和 .setAdapter 选项的自定义列表视图?

【问题讨论】:

  • 不,不需要DialogFragment(最好是imo)。 AlertDialog.Builder 具有 setAdapter 方法,因此您可以在 AlertDialog 中使用 ListView

标签: android listview android-alertdialog


【解决方案1】:

我遇到了同样的问题,查看了一些博客和网站,我发现 this 教程对我有很大帮助。 下面的代码是我的教程版本,我只在上面添加了一个复选框,用作您要求的多项选择项。

首先创建一个 ListActivity 作为你的对话框:

public class ListDialog extends ListActivity {

public String[] names;
private TypedArray imgs;
private List<DialogHelper> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    populateList();

    ArrayAdapter<DialogHelper> adapter = new ListDialogAdapter(this, list);
    setListAdapter(adapter);

    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}

private void populateList() {
    list = new ArrayList<>();
    names = getResources().getStringArray(R.array.names);
    imgs = getResources().obtainTypedArray(R.array.flags);
    for(int i = 0; i < names.length; i++){
        list.add(new DialogHelper(names[i], imgs.getDrawable(i)));
    }
  }
}

然后,创建一个类来帮助使用来自 string.xml 资源文件的资源填充列表。

public class DialogHelper {
private String name;
private Drawable flag;

public DialogHelper(String name, Drawable flag){
    this.name = name;
    this.flag = flag;

}
public String getName() {
    return name;
}

public Drawable getFlag() {
    return flag;
}
}

我们还需要一个 ListActiviy 的自定义适配器

public class ListDialogAdapter extends ArrayAdapter<DialogHelper> {

private final List<DialogHelper> list;
private final Activity context;

static class ViewHolder {
    protected TextView name;
    protected ImageView flag;
}

public ListDialogAdapter(Activity context, List<DialogHelper> list) {
    super(context, R.layout.dialog_layout, list);
    this.context = context;
    this.list = list;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.dialog_layout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.flag = (ImageView) view.findViewById(R.id.flag);
        view.setTag(viewHolder);
    } else {
        view = convertView;
    }

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.name.setText(list.get(position).getName());
    holder.flag.setImageDrawable(list.get(position).getFlag());
    return view;
  }
}

对于对话框布局,请使用下面的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" android:weightSum="10">

<ImageView
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:padding="7dp"
    android:id="@+id/flag">

</ImageView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Title"
    android:id="@+id/name"
    android:layout_gravity="center_vertical"
    android:layout_weight="10"
    android:layout_marginLeft="10dp"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox"
    android:checked="false"
    android:layout_gravity="center" />
</LinearLayout>

在您的 string.xml 中定义将填充您的列表对话框的数据。

<resources>
<string name="app_name">TestDialog</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<string-array name="names">
    <item>Apple</item>
    <item>Microsoft</item>
    <item>Motorola</item>
    <item>Dell</item>
</string-array>



<string-array name="flags">
    <item>@drawable/ic_apple</item>
    <item>@drawable/ic_microsoft</item>
    <item>@drawable/ic_motorola</item>
    <item>@drawable/ic_dell</item>
</string-array>
</resources>

在 manifest.xml 中将 ListDialog 主题定义为“对话框”

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdialog" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".ListDialog"
        android:theme="@android:style/Theme.Holo.Light.Dialog"
        android:noHistory="true"
        android:label="Select your company"></activity>
</application>
</manifest> 

要测试上面的代码,只需创建一个 Activity 来调用你的 listDialog

public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Intent intent = new Intent(this, ListDialog.class);
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
  }
 }

在 MainActivity 布局下方。

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Open Dialog"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

我希望这对你有帮助。

【讨论】:

    【解决方案2】:

    这个答案应该可以帮助你:Custom ListView in a dialog

    正如黑带所说,您可以使用“setAdapter”方法来设置您自己的自定义适配器。

    【讨论】:

      【解决方案3】:

      我会说对你来说最好的方法是制作一个自定义对话框。

      您可以使用本教程: http://www.mkyong.com/android/android-custom-dialog-example/

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 2012-09-17
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多