是否可以使用AlertDialog显示包含文本旁边图像的列表视图? 或者我是否必须使用listview实现DialogFragment(例如)?
修改 是否也可以使用带有选项.setMultiChoiceItems和.setAdapter的自定义列表视图?
答案 0 :(得分: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)));
}
}
}
然后,创建一个Class以帮助使用来自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>
我希望这会对你有所帮助。
答案 1 :(得分:0)
这个答案可以帮到你:Custom ListView in a dialog
正如blackbelt所说,您可以使用“setAdapter”方法设置自己的自定义适配器。
答案 2 :(得分:0)
我想说最好的方法是制作自定义对话框。
您可以使用本教程: http://www.mkyong.com/android/android-custom-dialog-example/
我希望它能帮到你:)。