android 自訂提示彈窗效果

來源:互聯網
上載者:User

轉自 http://blog.csdn.net/playboyanta123/article/details/8674653

java

package com.test.ui;import android.app.Activity;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class TestActivity extends Activity {/** Called when the activity is first created. */private Button but_menu;private Button open_id;private Button save_id;View contentView;private PopupWindow m_popupWindow;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);  setContentView(R.layout.main);  init();  setListener();}private void init() {  contentView = getLayoutInflater().inflate(R.layout.popupmenu, null,true);  but_menu = (Button) findViewById(R.id.but_menu);  open_id = (Button) contentView.findViewById(R.id.btn_popup_information);  save_id = (Button) contentView.findViewById(R.id.btn_popup_quote);// PopupWindow彈出的視窗顯示的view,第二和第三參數:分別表示此快顯視窗的大小m_popupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, true);  m_popupWindow.setBackgroundDrawable(new BitmapDrawable());//有了這句才可以點擊返回(撤銷)按鈕dismiss()popwindow  m_popupWindow.setOutsideTouchable(true);  m_popupWindow.setAnimationStyle(R.style.PopupAnimation);}private void setListener() {  contentView.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stub    m_popupWindow.dismiss();}});// m_popupWindow = new PopupWindow();  but_menu.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {if (m_popupWindow.isShowing()) {      m_popupWindow.dismiss();}     m_popupWindow.showAsDropDown(v);} catch (Exception e) {Toast.makeText(TestActivity.this, e.getMessage(),Toast.LENGTH_SHORT);}}});  open_id.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {    m_popupWindow.dismiss();Toast.makeText(TestActivity.this, "開啟被觸發", Toast.LENGTH_SHORT).show();}});  save_id.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {    m_popupWindow.dismiss();Toast.makeText(TestActivity.this, "儲存被觸發", Toast.LENGTH_SHORT).show();}});}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if (m_popupWindow != null && m_popupWindow.isShowing()) {    m_popupWindow.dismiss();return true;}}return super.onKeyDown(keyCode, event);}}

popupmenu.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/lin_main"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content" android:layout_width="wrap_content"android:paddingRight="120dip"><LinearLayout android:background="@drawable/popup_search"android:orientation="vertical" android:layout_height="wrap_content"android:layout_width="wrap_content"><Button android:layout_width="fill_parent" android:background="@drawable/goods_bg"android:id="@+id/btn_popup_information" android:layout_height="wrap_content"android:textColor="#000000" android:textSize="16sp" android:text="開啟"android:layout_weight="1" /><Button android:id="@+id/btn_popup_quote" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="儲存"android:background="@drawable/goods_bg" android:layout_weight="1"android:textColor="#000000" android:textSize="16sp"android:layout_marginTop="5dip" /><Button android:id="@+id/btn_popup_product"android:layout_width="fill_parent" android:layout_height="wrap_content"android:text="關於我們" android:background="@drawable/goods_bg"android:layout_weight="1" android:textColor="#000000"android:textSize="16sp" android:layout_marginTop="5dip" /><Button android:id="@+id/btn_popup_buy" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="關閉"android:background="@drawable/shop_bg" android:layout_weight="1"android:textColor="#000000" android:textSize="16sp"android:layout_marginTop="5dip" /></LinearLayout></LinearLayout>

下面的是popwindow出現和退出的動畫特效,這種東西網上有很多,就拿出這個吧.

values/style.xml

<?xml version="1.0" encoding="utf-8"?><resources><style name="PopupAnimation" parent="android:Animation"><item name="android:windowEnterAnimation">@anim/popup_search_show</item><item name="android:windowExitAnimation">@anim/popup_search_hide</item></style></resources>

anim/popup_search_show.xml

<?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/decelerate_interpolator"><scaleandroid:duration="500"android:pivotX="0.100000024%"android:pivotY="0.0"android:fromXScale="1.0"android:toXScale="0.0"android:fromYScale="1.0"android:toYScale="0.0"></scale></set>

anim/popup_search_hide.xml

<?xml version="1.0" encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/decelerate_interpolator"><scaleandroid:duration="500"android:pivotX="0.100000024%"android:pivotY="0.0"android:fromXScale="0.0"android:toXScale="1.0"android:fromYScale="0.0"android:toYScale="1.0"></scale></set>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.