我是安卓新手。这是交易。我想让我的活动像 GameCih 菜单栏(就像在所有活动之上,我可以绕过应用程序并做任何我想做的事情,并且菜单栏仍然在顶部)但我想做的与 GameCIH 不完全相同。我想要音乐播放器,这样我就可以播放歌曲并通过触摸在屏幕上显示专辑封面和移动播放器菜单。这是我的主要活动:
public class MainActivity extends Activity implements OnTouchListener{
RelativeLayout rel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
rel = (RelativeLayout) findViewById(R.id.myactivity);
rel.setOnTouchListener(this);
this.setFinishOnTouchOutside(false);
this.getWindow().setBackgroundDrawable(new ColorDrawable(0));
Window dialogWindow = this.getWindow();
// Make the dialog possible to be outside touch
dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
Thread th = new Thread(){
public void run (){
try {
while(true){
Thread.sleep(10000);
try{
Intent i=new Intent(getApplicationContext(),MainActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}catch(Exception ex){
Log.d("X",ex.getMessage());
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
th.start();
}
@Override
public void onBackPressed() {
// Do Here what ever you want do on back press;
}
@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;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if(rel==arg0){
switch(arg1.getAction())
{
case MotionEvent.ACTION_MOVE:
{
WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = ((int) arg1.getRawX()-400);
layoutParams.y = ((int) arg1.getRawY()-200);
this.getWindow().setAttributes(layoutParams);
return true;
}
case MotionEvent.ACTION_UP:
{
WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = ((int) arg1.getRawX()-400);
layoutParams.y = ((int) arg1.getRawY()-200);
this.getWindow().setAttributes(layoutParams);
return true;
}
case MotionEvent.ACTION_DOWN:
{
WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = ((int) arg1.getRawX()-400);
layoutParams.y = ((int) arg1.getRawY()-200);
this.getWindow().setAttributes(layoutParams);
return true;
}
}
}
return false;
}
}
和xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myactivity"
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/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="End" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dialogtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.dialogtest.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
游戏机
http://i.stack.imgur.com/RgivT.jpg
我的应用
http://i.stack.imgur.com/VvPcP.jpg
我的问题是:
- 1 我无法在顶部进行活动
- 2 可以触摸外部,但在某些应用程序中,当我触摸时不会发生任何事情
- 3 我曾经
startActivity(i);
在前台进行活动,但它会重新启动并在 2 中出现同样的问题
非常感谢您的帮助