0

ボタンを押したときに私のアクティビティにポップアップを表示したいと思います。私はインスピレーションを受けましたby this questionフラグメントなしのアクティビティでポップアップオーバーレイビューを作成するにはどうすればよいですか?

私はアクティビティのコンテンツxmlに "マージ"コントロールを使用し、2つの異なるレイアウトを入れましたが、問題は明らかにこの行にあります(上記のリンクから取ったコード):

FragmentManager fragmentManager = getFragmentManager(); 
fragmentManager.beginTransaction() 
    .add(R.id.overlay_fragment_container, yourFragment) 
    .commit(); 

ofc FragmentManagerはフラグメントに対して機能します。

私の問題は、自分のアクティビティが「断片化」されていないことですが、そのLinearLayoutはアクティビティ内のフラグメントではなく、アクティビティ内で直接膨張されます。

アクティビティでその質問のような同様の効果を得ることができますか、またはすべてのコントロールを強制的にフラグメントに埋め込む必要がありますか?

+0

[DialogFragment's](http://developer.android.com/reference/android/app/DialogFragment.html) –

+0

私のビューが複数のボタンだけであることを嫌う?コメントを選択するには、RatingBarとEditTextでレート表示が必要です。 DialogFragmentはそれに適していますか? (2つのボタンを追加してレートを調整したり、キャンセルしたりするので、そうかもしれません) – BlackBox

+0

ええ、[AlertDialog](http://developer.android.com/reference/android/app/AlertDialog.html)は標準ダイアログです確認などのために 'DialogFragment'ははるかに柔軟です(あなたが必要です)。 –

答えて

4

[OK]を事前に

おかげでこれは多くのコードのように見えるかもしれないが、それは本当に簡単です。

まず、XMLで必要なダイアログレイアウトを作成します。 (アクティビティビューと同じXMLではありません)次に例を示します。その後custom_dialog.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"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="52dp" 
     android:text="New Text" 
     android:id="@+id/txtTitle" /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     android:layout_weight="1" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="52dp"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/btnBtmLeft" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/btnBtmRight" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

、あなたの活動には、次の手順を実行します。その方法を

private void showMyDialog(Context context) { 
    final Dialog dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.custom_dialog); 
    dialog.setCanceledOnTouchOutside(false); 
    dialog.setCancelable(true); 

    TextView textView = (TextView) dialog.findViewById(R.id.txtTitle); 
    ListView listView = (ListView) dialog.findViewById(R.id.listView); 
    Button btnBtmLeft = (Button) dialog.findViewById(R.id.btnBtmLeft); 
    Button btnBtmRight = (Button) dialog.findViewById(R.id.btnBtmRight); 

    btnBtmLeft.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 

    btnBtmRight.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // do whatever you want here 
     } 
    }); 

    /** 
    * if you want the dialog to be specific size, do the following 
    * this will cover 85% of the screen (85% width and 85% height) 
    */ 
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 
    int dialogWidth = (int)(displayMetrics.widthPixels * 0.85); 
    int dialogHeight = (int)(displayMetrics.heightPixels * 0.85); 
    dialog.getWindow().setLayout(dialogWidth, dialogHeight); 

    dialog.show(); 
} 

そして最後に、あなたの活動ののonCreateで、呼び出し

myButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     showMyDialog(context); 
    } 
}); 

これが役立つことを願っています!

+0

これは素晴らしいです! – BlackBox

関連する問題