【发布时间】:2014-12-31 05:31:30
【问题描述】:
我正在开发一个应用程序,在该应用程序中,当收到呼叫时应该出现一个浮动组件,并且该组件将有几个按钮来执行必要的操作。 我已经尝试了以下。 我通过使主要活动半透明来实现一个弹出窗口。当这个组件弹出时,我可以在屏幕上移动它,但由于活动是半透明的,我无法执行任何other 活动。
在这里你可以看到弹出窗口,我可以移动它,但我不能在后台滚动菜单抽屉。我怎样才能以一种可以同时执行两种操作的方式实现,即在弹出窗口和背景屏幕上。
我的代码
`public class MainActivity extends Activity {
int mCurrentX;
int mCurrentY;
private float mDx;
private float mDy;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final View cv = new View(this);
TextView tv = new TextView(this);
tv.setBackgroundColor(0xffeeeeee);
tv.setTextColor(0xff000000);
tv.setTextSize(24);
tv.setText("click me\nthen drag me");
tv.setPadding(8, 8, 8, 8);
final PopupWindow mPopup = new PopupWindow(tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
mDx = mCurrentX - event.getRawX();
mDy = mCurrentY - event.getRawY();
} else
if (action == MotionEvent.ACTION_MOVE) {
mCurrentX = (int) (event.getRawX() + mDx);
mCurrentY = (int) (event.getRawY() + mDy);
mPopup.update(mCurrentX, mCurrentY, -1, -1);
}
return true;
}
};
tv.setOnTouchListener(otl);
mCurrentX = 20;
mCurrentY = 50;
cv.post(new Runnable() {
@Override
public void run() {
mPopup.showAtLocation(cv, Gravity.NO_GRAVITY, mCurrentX, mCurrentY);
}
});
}
}`
清单
`<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="PopupMainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.FloatingWindow.Popup"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
android:clearTaskOnLaunch="true"
android:exported="true"
tools:ignore="ExportedActivity" />
</application>`
请帮助我。我想总体上实现一种小部件类型的组件。谢谢!!
【问题讨论】:
标签: android popupwindow