2

私はアラートダイアログで遊んでいます。リストビューのリストアイテムに関する特定の情報を表示するダイアログを表示したい。Androidのファイルマネージャーの詳細ダイアログと同じように。

写真:https ://dl.dropbox.com/u/20856352/detailsbox.jpg

この[詳細]ダイアログの興味深い点は、[設定]画面の[設定]アイテムと非常によく似たリストアイテムが表示されることです。クリックすると、非常に見事にレイアウトされた2行のアイテムリストアイテムが表示されます。

同様のダイアログボックスを作成する必要がありますが、これを実現する方法がわかりません。少し遊んだことがあります。プリファレンスXMLをalertdialogのレイアウトとして使用することはできません。そして、上の写真のようなレイアウトを開発することはできません。これを達成する方法のヘルプ/ガイドラインが必要です。

ファラズ・アズハル

4

4 に答える 4

5

AlertDialogの外観を複製するのは難しいため、カスタムダイアログを使用することはおそらく望ましくありません。AlertDialogは、AlertDialog.setListAdapterを使用してアイテムのリストを表示できます。ListAdapterのカスタム実装を使用して、アイテムのリストをカスタマイズし、アイテムごとに2行のテキストを表示できます。添付のスクリーンショットは、以下のコードと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>
于 2012-07-20T19:04:16.150 に答える
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がルートxml idです(例:'<' linearlayout android:id = "+ @ id ...."'>')

見せたいときはこの行を書いてください

alertDialog.show();

于 2012-07-20T18:02:08.597 に答える
0

私の提案は、アクティビティをダイアログとして使用することです。このようにして、カスタムダイアログを作成するのは非常に簡単です。これは、あなたが構築できると思う小さな例です。

**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>
于 2012-07-20T18:18:30.563 に答える
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()
于 2012-07-20T19:07:49.257 に答える