我一直在玩警报对话框。我想显示一个对话框,显示listview中列表项的特定信息。就像android的文件管理器的详细对话框一样。
图片:https://dl.dropbox.com/u/20856352/detailsbox.jpg
有关此详细信息对话框的有趣之处在于,它显示的列表项与“首选项”屏幕中的“首选项”非常相似。可以点击它们,它们显示了一个非常精心布置的两行项目列表项。
我需要创建一个类似的对话框,但我不知道如何实现这一目标。我玩了一下。首选项XML不能用作alertdialog的布局。而且我无法开发出类似于上面图片的布局。需要帮助/指南如何实现这一目标。
Faraz Azhar
答案 0 :(得分:5)
您可能不想使用自定义对话框,因为很难复制AlertDialog的外观。 AlertDialog可以使用AlertDialog.setListAdapter显示项目列表。您可以使用ListAdapter的自定义实现自定义项目列表,以显示每个项目的两行文本。附带的屏幕截图由以下代码和xml生成。
public class Temp extends Activity
{
private String[] listItemsFirstRow = {"item 1", "item 2", "item 3"};
private String[] listItemsSecondRow = {"item 1", "item 2", "item 3"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setAdapter(new MyAdapter(), null);
builder.setTitle("Title");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
class MyAdapter extends BaseAdapter
{
@Override
public int getCount()
{
return listItemsFirstRow.length;
}
@Override
public Object getItem(int position)
{
//this isn't great
return listItemsFirstRow[position];
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
convertView = getLayoutInflater().inflate(R.layout.main, null);
}
((TextView)convertView.findViewById(R.id.text1)).setText( listItemsFirstRow[position]);
((TextView)convertView.findViewById(R.id.text2)).setText( listItemsSecondRow[position]);
return convertView;
}
}
}
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:minHeight=![enter image description here][2]"?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="15dip"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 1 :(得分:0)
刚刚将xml文件与常规屏幕/页面相同
然后将此代码放在onCreate()
上AlertDialog.Builder builder;
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_info,
(ViewGroup) findViewById(R.id.toast_layout_root));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
alertDialog = builder.create();
其中r.layout.toast_info是您的xml文件 和r.id.toast_layout_root是你的root xml id(例如&#39;&lt;&#;; linearlayout android:id =&#34; + @ id ....&#34;&#39;&gt; &#39;)
当你想要显示它时只需写下这一行
alertDialog.show();
答案 2 :(得分:0)
我的建议是将Activity用作对话框。这样,创建自定义对话框非常容易。这是一个我认为你可以建立的小例子。
**Activity**
public class CustomDialogEx extends Activity implements OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) { // pass your string data via this intent to the custom view
// show the custom dialog
Intent i = new Intent();
// i.putExtra(<your key/value pairs here>
i.setClass(this, DialogActivity.class);
startActivity(i);
}
}
****************************************************************************
**Custom Dialog**
// The Activity will serve as the Dialog
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.your_dialog_layout);
Intent i = new Intent();
Bundle b = getIntent().getExtras();
b.getString(<your key>)
}
}
*****************************************************************************
**AndroidManifest**
<activity
android:name="DialogActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.Dialog" >
</activity>
答案 3 :(得分:0)
this may also help you
public class ShareDialog extends Dialog implements android.view.View.OnClickListener{
Context mcontContext;
Button btnok;
Listview lstview;
public ShareDialog(Context context) {
super(context);
mcontContext= context;
//pls replace with your dialog.xml file
setContentView(R.layout.sharedialog);
bindComponent();
addListener();
}
private void bindComponent() {
// TODO Auto-generated method stub
lstview=(Listview) findViewById(R.id.lstdetail);
btnok=(Button) findViewById(R.id.btnok);
//bind here listview with your adpter
}
private void addListener()
{
btnshareviwifi.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnok:
dismiss();
break;
default:
break;
}
}
}
and where you want to show
ShareDialog shobje=new ShareDialog (context);
shobje.show()