2017-03-27 68 views

回答

0
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View popupView = layoutInflater.inflate(define your layout here, null); 
     final PopupWindow popupWindow = new PopupWindow(
      popupView, 
      LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT); 

     Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss); 
     btnDismiss.setOnClickListener(new Button.OnClickListener(){ 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    popupWindow.dismiss(); 
}}); 

     popupWindow.showAsDropDown(btnOpenPopup, 50, -30); 

}}); 
+0

它會顯示如圖所示@Akash? –

+0

,因爲我非常重視相關的UI –

+0

是的,它會顯示您的圖片 – Akash

0
<?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:layout_gravity="center" 
      android:background="@color/FB_White" 
      android:gravity="center" 
      android:orientation="vertical" 
      android:padding="10dp"> 

      <TextView 
       android:id="@+id/txtTitle" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:text="Title" 
       android:textColor="@color/FB_Black" 
       android:textSize="14sp" /> 

      <!-- <TextView 
       android:id="@+id/txtMsg" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:layout_marginTop="10dp" 
       android:padding="5dp" 
       android:text="Content" 
       android:textColor="@color/FB_White" 
       android:textSize="12sp" />--> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="right"> 

       <Button 
        android:id="@+id/btnCancel" 
        android:layout_width="wrap_content" 
        android:layout_height="40dp" 
        android:layout_gravity="center" 
        android:background="@drawable/background_btncancel" 
        android:gravity="center" 
        android:text="CANCEL" 
        android:textColor="@color/FB_Orange" 
        android:textSize="12sp" /> 

       <Button 
        android:id="@+id/btnOk" 
        android:layout_width="wrap_content" 
        android:layout_height="40dp" 
        android:layout_gravity="center" 
        android:layout_marginLeft="5dp" 
        android:background="@drawable/background_btnok" 
        android:gravity="center" 
        android:text="OK" 
        android:textColor="@color/FB_Orange" 
        android:textSize="12sp" /> 


      </LinearLayout> 
     </LinearLayout> 

**Show alertDialog** 


final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
           MainActivity.this); 
         LayoutInflater inflater = (LayoutInflater) MainActivity.this 
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         View view = inflater.inflate(R.layout.custom_alertdialog, null); 
         alertDialogBuilder.setView(view); 
         alertDialogBuilder.setCancelable(true); 
         final AlertDialog dialog = alertDialogBuilder.create(); 
         dialog.show(); 

         TextView txtTitle = (TextView) dialog.findViewById(R.id.txtTitle); 
         txtTitle.setText("Proceed to Logout?"); 
         txtTitle.setTypeface(common.setTypefaceRegular()); 

         Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel); 
         btnCancel.setText("CANCEL"); 
         btnCancel.setTypeface(common.setTypefaceSemiBold()); 
         btnCancel.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           dialog.dismiss(); 
          } 
         }); 

         Button btnOk = (Button) dialog.findViewById(R.id.btnOk); 
         btnOk.setText("OK"); 
         btnOk.setTypeface(common.setTypefaceSemiBold()); 
         btnOk.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           dialog.dismiss(); 
           deleteLogin(); 
           Intent i = new Intent(MainActivity.this, LoginActivity.class); 
           startActivity(i); 
           finish(); 
          } 
         }); 
         dialog.show(); 
+0

它會顯示,因爲它顯示在圖像? @Punithapriya –

+0

不,我只是添加了示例代碼,您可以添加您想要的視圖。它很簡單。 – Punithapriya

+0

謝謝生病嘗試並更新你。 –

0

在(...)按鈕點擊動作添加以下行

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, x, y, 0);   
    PopupWindow popup = new PopupWindow(context); 
     popup.setContentView(inflater.inflate(R.layout.yourlayout, null, false)); 
     popup.setWidth(300); 
     popup.setHeight(150); 
     popup.setFocusable(true); 
     popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    popup.show(event); 

其中R.layout.yourlayout,何況你需要在彈出的窗口中顯示的佈局。

相關問題