先上效果图:

android怎样写一个自己定义的dialog能够在Title的位置弹出来-LMLPHP

Title的Layout为:

<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="@dimen/title_height"
android:background="@drawable/bg_top_title"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="20sp"
android:layout_centerInParent="true"/> <ImageView
android:layout_width="@dimen/header_btn_width"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_alignParentTop="true"
android:paddingLeft="4dp"
android:id="@+id/right_button"
android:src="@drawable/arrow_dropdown_pressed"
android:layout_alignParentRight="true"
/>
</RelativeLayout>

弹出的dialog的Layout为

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/dropdownBckgrnd"
android:background="@drawable/bg_pop_up_dimmer"
> <ImageView
android:layout_width="@dimen/header_btn_width"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_alignParentTop="true"
android:paddingLeft="4dp"
android:id="@+id/right_button"
android:src="@drawable/arrow_dropdown_pressed"
android:layout_alignParentRight="true"
/> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="35dp"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/topmenu_popup_down"> <Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/main_menu_button_background"
android:layout_margin="@dimen/button_margin_top"
android:text="aaa"
/> <Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/main_menu_button_background"
android:layout_margin="@dimen/button_margin_top"
android:text="bbb"
/> <Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/main_menu_button_background"
android:layout_margin="@dimen/button_margin_top"
android:text="ccc"
/>
</LinearLayout>
</RelativeLayout>

我们使用

 <span style="white-space:pre">	</span>mDialog = new Dialog(context,R.style.customDialog);
mDialog.setContentView(R.layout.dialog_layout);
<style name="customDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item> //设置title
<item name="android:windowBackground">@android:color/transparent</item> //dialog应该是透明背景
<item name="android:windowIsFloating">false</item> //dialog不是悬浮的
<item name="android:layoutAnimation">@null</item> //dialog弹出时没有动画
</style>

然后我们来设置Dialog的其它属性

        mDialog = new Dialog(context,R.style.customDialog);
mDialog.setContentView(R.layout.dialog_layout);
mDialog.setCanceledOnTouchOutside(true);
WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
params.gravity = Gravity.TOP;//这个设置使这个dialog从上方弹出来
params.windowAnimations = 1; WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
windowHeight = display.getHeight();
windowWidth = display.getWidth();
params.width = windowWidth;
params.height = windowHeight; mDialog.findViewById(R.id.right_button).setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
mDialog.dismiss();
}
}); mDialog.findViewById(R.id.dropdownBckgrnd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});

04-28 23:37