简单的实现了底部弹出dialog的实现效果 直接上代码
activity中代码
package com.example.dialogdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button button;
private MyDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDialog(View view) {
dialog = new MyDialog(this);
dialog.setContentView(R.layout.dialog);
WindowInfo windowInfo = new WindowInfo(this);
dialog.getWindow().setLayout(windowInfo.getWindowWidth(),
windowInfo.getWindowHeight() / 2);
dialog.show();
button = (Button) dialog.findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
}
activity布局文件
<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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="showDialog"
android:text="@string/hello_world" />
</RelativeLayout>
自定义dialog
package com.example.dialogdemo;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.Window;
public class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setBackgroundDrawable(new ColorDrawable(0xFFFFFF));
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.mystyle);
}
}
dialog布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
style代码
<style name="mystyle" parent="android:Animation">
<item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="@android:windowExitAnimation">@anim/dialog_exit</item>
</style>
动画代码
anim目录下 dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromYDelta="100%p" />
</set>
anim目录下 dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:toYDelta="100%p" />
</set>
ok完成了
点击demo下载
效果图