我正在制作一个闹钟应用。 我想要创建一个只有背景图片的活动页面。 当我在任意地方触摸屏幕时,音乐应该停止播放。
我考虑将图片作为ImageView展示... 但是无论如何设置参数,它都不能充满整个屏幕。
求助!
我正在制作一个闹钟应用。 我想要创建一个只有背景图片的活动页面。 当我在任意地方触摸屏幕时,音乐应该停止播放。
我考虑将图片作为ImageView展示... 但是无论如何设置参数,它都不能充满整个屏幕。
求助!
在您的页面布局中,请验证以下内容:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
如果想要实现该功能,可以尝试使用onTouchListener。
接下来,请尝试:
yourActivityLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// action to do
return true;//always return true to consume event
}
});
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// do your work here
return true;
} else {
return false;
}
}
}
@override
public void onClick(view v)
{
if(isMusicPlaying)
stopMusic();
// here run the rest of your logic
if (v == someButton){}
}
你可以使用WindowManager并将你的布局覆盖在其他所有内容上(也可以覆盖状态栏和其他系统UI)
WindowManager实例具有addView()方法。通过正确的布局参数,它可以产生所描述的结果。
试试这个,将您的最顶层父视图作为参数传递给此方法
例如:stopMusicOnTouch(您的父视图);
public void stopMusicOnTouch(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof ImageView)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Stop music here
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
stopMusicOnTouch(innerView);
}
}
}