步步為營_Android開發課[26]_使用者介面之PopupWindow(快顯視窗)

來源:互聯網
上載者:User

標籤:popupwindo   android   使用者介面   開發   

Focus on technology, enjoy life!—— QQ:804212028
瀏覽連結:http://blog.csdn.net/y18334702058/article/details/44624305

  • 主題:使用者介面之PopupWindow(快顯視窗)
    -

PopupWindow和AlertDialog的區別:

AlertDialog是非阻塞式的:AlertDialog彈出時,後台還可以做事情。

PopupWindow是阻塞式的:在PopupWindow退出前,只允許我們操作PopupWindow,其他動作被阻塞。

PopupWindow(執行個體):

給PopupWindow定義一個布局。
popupwindow.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#336699"    android:orientation="vertical" >    <ImageView        android:id="@+id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/myimage" />    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical" >        <Button            android:id="@+id/button1_sure"            android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:text="確定"/>        <Button android:id="@+id/button2_cancel"             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="取消"            />    </LinearLayout></LinearLayout>

MainActivity.java:

import android.app.Activity;import android.os.Bundle;import android.text.InputType;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.PopupWindow;import android.widget.TextView;public class MainActivity extends Activity {     Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button1);        button.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                initPopWindow();            }               });    }    private void initPopWindow() {         // 載入PopupWindow的布局檔案        View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popupwindow, null);         // 聲明Popupwindow快顯視窗        PopupWindow popupWindow = new PopupWindow(null,100,300);        popupWindow.setContentView(contentView);        //獲得焦點        popupWindow.setFocusable(true);         /**          * popupWindow的三個顯示方法:         * showAsDropDown(View view)彈出對話方塊,位置在緊挨著view組件           showAsDropDown(View anchor, int x, int y)彈出對話方塊,位置在緊挨著view組件,x y代表位移量           showAtLocation(View parent, int gravity, int x, int y)彈出對話方塊,位置相對於父布局的位置,x y代表位移量         */        popupWindow.showAsDropDown(button);        ImageView imageview = (ImageView) contentView.findViewById(R.id.imageview);        Button button_sure = (Button) contentView.findViewById(R.id.button1_sure);        Button button_cancel = (Button) contentView.findViewById(R.id.button2_cancel);    }}

運行結果:

Focus on technology, enjoy life!—— QQ:804212028
瀏覽連結:http://blog.csdn.net/y18334702058/article/details/44624305

步步為營_Android開發課[26]_使用者介面之PopupWindow(快顯視窗)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.