2016-05-23 2 views
1

私はいくつかのメニューボタンを持っている主な活動があります。ユーザーが特定のボタンを押すと、新しいactivityを開きます。このボタンには、クリックを処理するためのボタンが2つあります。言い換えれば、私は通常の活動としてウィンドウの機能をポップアップする必要があります。
オンラインで見て、activityのサイズをカスタマイズし、マニフェストでdiaglogテーマを使用し、fragmentとして使用するか、Popup Windowクラスを使用するなど、これを実装する方法がいくつか見つかりました。しかし、私はアンドロイドに慣れているので、私は自分のプロジェクトのためにそれを実装する最良の方法を欲しがっています。
誰かがこれを達成するのを手助けできますか?アンドロイドアプリケーションでポップアップウィンドウを実装する最善の方法

EDIT: これは私が(私は達成するために何をしたいのより良い説明のための)ポップアップウィンドウで使用する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:gravity="center_horizontal" 
    android:orientation="vertical" 
    android:background="#0091cb" 
    android:padding="16dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingTop="@dimen/activity_horizontal_margin" 
     android:weightSum="3" 
     android:id="@+id/check"> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/computer" 
      android:paddingTop="12dp" 
      android:layout_marginLeft="10dp" 
      android:text="button1" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:layout_weight="1" 
      android:onClick="button1_OnClick"/> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/electrical" 
      android:paddingTop="12dp" 
      android:layout_marginLeft="10dp" 
      android:text="button2" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:layout_weight="1" 
      android:onClick="button2_OnClick"/> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/hdtv" 
      android:paddingTop="12dp" 
      android:layout_marginLeft="10dp" 
      android:text="button3" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:layout_weight="1" 
      android:onClick="button3_OnClick"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:orientation="horizontal" 
     android:layout_below="@id/check" 
     android:paddingTop="10dp"> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/bill" 
      android:paddingTop="12dp" 
      android:layout_marginLeft="10dp" 
      android:text="button4" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:onClick="button4_OnClick"/> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/water" 
      android:paddingTop="12dp" 
      android:text="button5" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:onClick="button5_OnClick" /> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/electrical" 
      android:paddingTop="12dp" 
      android:text="button6" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:onClick="button6_OnClick" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="85dp" 
      android:layout_height="85dp" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/circle" 
      android:drawableTop="@drawable/notepad" 
      android:paddingTop="7dp" 
      android:text="button7" 
      android:textSize="10dp" 
      android:textColor="#fff" 
      android:onClick="button7_OnClick" /> 
     </LinearLayout> 
</LinearLayout> 
+2

ここをクリックしてください。https://developer.android.com/reference/android/widget/PopupWindow.html and stackoverflow research – okarakose

+0

ありがとうございます。私はこれらの方法を見つけました。活動のサイズをカスタマイズし、マニフェストでdiaglogテーマを使用して、フラグメントとして使用するか、ポップアップウィンドウクラスを使用します。しかし、私は何のために最善の方法がわからない –

+0

このメソッドを使用してポップアップウィンドウを開きます。私のapp.iで使用されているこのメソッドは、私も好きです。 –

答えて

0

uが開くようにしたいメソッドを作成しますあなたの活動のポップアップウィンドウ このように、ここでは、pはあなたのウィンドウを正確に開く特定のポイント(場所)です。

final Point p = new Point(); 
    show_popup.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       showpopupwindows(Activity, p); 

      } 
     }); 

その後、

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    int location[] = new int[2]; 

    show_popup.getLocationOnScreen(location); 
    p.x = location[0]; 
    p.y = location[1]; 
} 

private void showpopupwindows(final Activity context, Point p) { 

    LinearLayout viewGroup = (LinearLayout) context 
      .findViewById(R.id.popup_menu); 

    LayoutInflater layoutInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    Display display = context.getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 

    int popupwidth = (size.x/2); 
    int popupheight =(size.y/2); 

    View layout = layoutInflater.inflate(R.layout.detail_pop, viewGroup); 

    final PopupWindow popup = new PopupWindow(context); 

    popup.setContentView(layout); 
    popup.setWidth(popupwidth); 
    popup.setHeight(popupheight); 
    popup.setFocusable(true); 
    popup.setAnimationStyle(R.style.WindowAnimation); 
    popup.setBackgroundDrawable(new ColorDrawable(
      android.graphics.Color.TRANSPARENT)); 

    popup.showAtLocation(layout, Gravity.NO_GRAVITY, popupwidth,popupheight); 

    detail_pop1 = (TextView) layout.findViewById(R.id.detail_pop1); 

    detail_pop1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getContext(), "pop window is opened, Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 

detail_pop.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popup_menu" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:background="@color/skyblue" 
    android:gravity="center" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:id="@+id/detail_pop1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_weight="1" 
     android:drawableTop="@drawable/ic_launcher" 
     android:gravity="center" 
     android:text="P" 
     android:textSize="@dimen/font_24" /> 


</LinearLayout> 
+0

ここに私のコードで私のレイアウト名はdetail_popだからあなたのレイアウト名をライトして、すべてのボタンにidを与えて、定義できるように定義することもできます。あなたがボタンとして演奏するものは何でも、私はそうすることができます。 –

+0

これらのオブジェクトは何ですか:LinearLayout viewGroup =(LinearLayout)context .findViewById(R.id.popup_menu); LayoutInflater layoutInflater =(LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);あなたの渡すshowPopUpWindow()の空のポイントは、onWindowFocusChanged()で初期化されます。どのように動作するかの論理を説明できますか? –

+0

ここpopup_menuはメインの線形レイアウトのidです。あなたは主な線形レイアウトにidを与えます.initiallly私はちょうどポイントを定義しましたが、私はpoint.plzの位置を記述するmethosで注意深く見てください。あなたのプロジェクトにいくつかの変更を加える。 –

0

は、あなたのニーズによって異なります。
「クラシック」ポップアップウィンドウ(入力フィールドや小さなカラーピッカーで入力中にエラーが発生する)を表示する必要がある場合は、PopupWindowクラスを使用してください。 my gistを使用できます。
メッセージが一般的な場合(「インターネット接続がありません」など)、ユーザーは「はい/いいえ」を選択する必要があります。 cool libraryがあります。
カスタムサイズのアクティビティはめったに使用されません。たぶん一部のダイヤルアプリケーションで。 あなたのニーズに応じて実装を選択してください。

+0

私の必要性は、メッセージやアラートを表示するために、通常のアクティビティのように処理することです。アクティビティで私はポップアップとして表示したい私は自分の活動の中で関数を使ってクリックイベントを処理する必要があるボタンを持っています –

+0

@ A.Tこれらの方法のいずれかを使ってあなたのポップアップレイアウトにボタンを持つことができます – rocknow

関連する問題