2013-12-16 7 views
-1

Я совершенно новый в android. Мне нужно всплывающее окно в моем приложении. У меня есть TextView и кнопки в моем файле activity_main.xml, и TextView и две кнопки в файле popup.xml вот эти файлыPopUp Window не появляется, когда я нажимаю

popup.xml file 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/popup_element" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 


<TextView 
    android:id="@+id/txt1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:text="This is popoup" /> 
</TableRow> 

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 


<Button 
    android:id="@+id/Unpair" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Unpair" /> 

<Button 
    android:id="@+id/Cancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="cancel" /> 
</TableRow> 
</TableLayout> 

Вот файл activity_main.xml

activity_main.xml 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<Button 
    android:id="@+id/btn1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="18dp" 
    android:layout_marginTop="28dp" 
    android:layout_toRightOf="@+id/textView1" 
    android:text="Cick me" /> 

</RelativeLayout> 

Я хочу отобразить всплывающее окно, когда я нажму кнопку «кликнуть меня». Но всплывающее окно не появляется, когда я делаю нажмите кнопку

The java code 

package com.example.ctrckerapp; 

import android.os.Bundle; 
import android.widget.*; 
import android.app.Activity; 
import android.view.*; 
import android.content.*; 
public class MainActivity extends Activity { 

private PopupWindow pw; 
private Button Cancel; 
private Button Unpair; 
private Button Click; 
View layout; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Click=(Button)findViewById(R.id.btn1); 
    Click.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       initiatePopup(); 
      } 
       }); 
    try{ 
    Cancel.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       pw.dismiss(); 
      } 
      }); 


    layout.post(new Runnable(){ 
     public void run() 
     { 
     pw.showAtLocation(layout, Gravity.BOTTOM, 100, 700); 
     } 
    }); 

}catch(Exception e){ 
    Toast.makeText(getApplicationContext(),  
e.toString(),Toast.LENGTH_LONG).show(); 
} 


} 

public void initiatePopup() 
{ 
    try{ 
    LayoutInflater inflater=  
(LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layout=inflater.inflate(R.layout.popup, 
(ViewGroup)findViewById(R.id.popup_element)); 
    pw=new PopupWindow(layout,300,670,true); 
    Cancel =(Button)layout.findViewById(R.id.Cancel); 
    Unpair=(Button)layout.findViewById(R.id.Unpair); 


    }catch(Exception e){ 
     Toast.makeText(getApplicationContext(), 
e.toString(),Toast.LENGTH_LONG).show(); 
    } 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

ответ

0

вы можете проверить это метод создания основного Всплывающего

http://www.androidhub4you.com/2012/07/how-to-create-popup-window-in-android.html

кстати просто проверить свой код еще раз и попытаться месте следуя строке в initiatePopup() и вызывая ее всякий раз, когда вы хотите всплывать.

pw.showAtLocation(layout, Gravity.BOTTOM, 100, 700); 

возможно всплывающее окно тратится, поэтому попробовать это также,

pw.showAtLocation(layout, Gravity.CENTER, 0, 0); 
0

я мог бы быть поздно к партии, но попробовать это.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
      pw.showAsDropDown(context.getCurrentFocus(), 0, 0,Gravity.CENTER_HORIZONTAL|Gravity.TOP); 
     }else{ 
      pw.showAtLocation(context.getCurrentFocus(), Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 10, 10); 

     } 

context.getCurrentFocus() может быть заменен любым view..its просто просмотреть

также могут wonâ добавить pw.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

Смежные вопросы