Android PopupWindow使用樣本

來源:互聯網
上載者:User

標籤:

樣本效果如下:

    

MainActivity.xml

package sn.qdj.popupwindowdemo;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.PopupWindow;/** * PopupWindow使用 * @author qingdujun * */public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        /**         * popup.xml 為彈出介面布局         */        View root = getLayoutInflater().inflate(R.layout.popup, null);                /**         * 彈出介面         * 寬度:400         * 高度:200         */        final PopupWindow popup = new PopupWindow(root, 400,200);        Button btn = (Button)findViewById(R.id.btn);        Button close = (Button)findViewById(R.id.close);                btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub/** * 在指定位置彈出 *  * 第一個參數指定PopupWindow的錨點view,即依附在哪個view上。 * 第二個參數指定起始點為parent的右下角 * 第三個參數設定以btn的下方為原點,向左、上各位移0像素。 */popup.showAtLocation(findViewById(R.id.btn), Gravity.BOTTOM, 0, 0);}});        close.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub/** * 關閉PopupWindow */popup.dismiss();}});    }}
activity_main.xml

<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" >    <Button        android:id="@+id/btn"        android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:text="彈出" />    <Button        android:id="@+id/close"        android:layout_width="200dp"        android:layout_height="wrap_content"android:layout_toRightOf="@id/btn"        android:text="關閉" /></RelativeLayout>

popup.xml

<?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:orientation="vertical" >    <Button        android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="相簿" />    <Button        android:id="@+id/btn2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="拍照" /></LinearLayout>

源碼下載,請點擊這裡!

Android 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.