请问 如下代码,当点击imageview_popwindow 按钮的时候 ,为什么没有在imageview_popwindow 下方出现popupwindow呢?(也就是点击后,没有反应)
麻烦会的朋友帮忙回复下,谢谢啦
public class TestActivity extends Activity {
。。。
private PopupWindow mPopupWindow;
private ImageView imageview_popwindow;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//imageview_popwindow 按钮
imageview_popwindow = (ImageView) findViewById(R.id.imageview_popwindow);
imageview_popwindow.setOnClickListener( new OnClickListener() {
public void onClick(View v){
Log.d(TAG,"yong+oncreate:imageview_popwindow onClick() ");
getPopupWindowInstance();
mPopupWindow.showAsDropDown(imageview_popwindow ); // 想在imageview_popwindow 下方弹出 popupwindow
}
});
}
private void getPopupWindowInstance() {
if( mPopupWindow != null) {
mPopupWindow.dismiss();
} else {
initPopupWindow();
}
}
private void initPopupWindow() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.popup_window, null); // 布局文件 popup_window.xml 里边有个textview
mPopupWindow = new PopupWindow(popupWindow,100,130);
}
------解决思路----------------------
可以弹出来啊
Activity
package app.example.t;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
private PopupWindow mPopupWindow;
private ImageView imageview_popwindow;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// imageview_popwindow 按钮
imageview_popwindow = (ImageView) findViewById(R.id.imageview_popwindow);
imageview_popwindow.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getPopupWindowInstance();
mPopupWindow.showAsDropDown(imageview_popwindow); // 想在imageview_popwindow
// 下方弹出
// popupwindow
}
});
}
private void getPopupWindowInstance() {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
} else {
initPopupWindow();
}
}
private void initPopupWindow() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.popup_window, null); // 布局文件
// popup_window.xml
// 里边有个textview
mPopupWindow = new PopupWindow(popupWindow, 100, 130);
}
}
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" >
<ImageView
android:id="@+id/imageview_popwindow"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#ff33cc" />
</RelativeLayout>
popup_window.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="一本正经" >
</TextView>
------解决思路----------------------
点击ImageView前
点击ImageView后
------解决思路----------------------
pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
R.drawable.patientgroup_selected));// 设置背景图片,不能在布局中设置,要通过代码来设置
设置一下pwMyPopWindow的背景图片,我以前也遇到过,测试好多遍,才测试出给他设置背景图片就可以弹出来了,你试试吧我估计你就是这个问题