[Android development diary] Popupwindow perfect demo, androidpopupwindow

Source: Internet
Author: User

[Android development diary] Popupwindow perfect demo, androidpopupwindow
Popupwindow perfect demo implementation
Figure:

Key Code Description:

1. popupwindow is displayed, and the background is dimmed.

ColorDrawable cd = new ColorDrawable(0x000000);popuWindow1.setBackgroundDrawable(cd); WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha = 0.4f;getWindow().setAttributes(lp);

2. After the popupwindow disappears, the background is restored.

public void onDismiss(){    WindowManager.LayoutParams lp=getWindow().getAttributes();    lp.alpha = 1f;    getWindow().setAttributes(lp);}

3. popupwindow rounded rectangle

Rounded_corners_bg.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">     <solid android:color="#FFFFFF" />     <corners         android:topLeftRadius="5dp"        android:topRightRadius="5dp"         android:bottomRightRadius="5dp"        android:bottomLeftRadius="5dp"/> </shape>
Set background in pupopwindow layout File

<?xml version="1.0" encoding="UTF-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:background="@drawable/rounded_corners_bg"    android:orientation="vertical" >     ...     ...     ... </RelativeLayout >

4. Click the outside of popupwindow, and popupwindow will also dismiss

popuWindow1.setOutsideTouchable(true);popuWindow1.setFocusable(true);

Complete code:

1. MainActivity. java

Package com. popupwindowdemo; import android. app. activity; import android. content. intent; import android. graphics. drawable. colorDrawable; import android.net. uri; import android. OS. bundle; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. view. windowManager; import android. widget. button; import android. widget. popupWindow; import android. widget. textView; import android. widget. popupWindow. extends; public class MainActivity extends Activity {// popupwindowprivate PopupWindow popuWindow1; private View contentView1; private TextView textview1; private Button btn1; @ Overrideprotected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textview1 = (TextView) findViewById (R. id. textView2); btn1 = (Button) findViewById (R. id. button1); btn1.setOnClickListener (new View. onClickListener () {public void onClick (View v) {initPopuWindow1 (v) ;}}); textview1.setOnClickListener (new View. onClickListener () {public void onClick (View v) {final Intent intent = new Intent (Intent. ACTION_VIEW, Uri. parse ("http://blog.csdn.net/geeklei/article/"); startActivity (intent) ;}});} private void initPopuWindow1 (View parent) {if (popuWindow1 = null) {LayoutInflater mLayoutInflater = LayoutInflater. from (this); contentView1 = mLayoutInflater. inflate (R. layout. popuwindow1, null); popuWindow1 = new PopupWindow (contentView1, ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT);} ColorDrawable cd = new ColorDrawable (0x000000); popuWindow1.setBackgroundDrawable (cd); // generate the background darkening effect WindowManager. layoutParams lp = getWindow (). getAttributes (); lp. alpha = 0.4f; getWindow (). setAttributes (lp); popuWindow1.setOutsideTouchable (true); popuWindow1.setFocusable (true); popuWindow1.showAtLocation (View) parent. getParent (), Gravity. CENTER | Gravity. CENTER_HORIZONTAL, 0, 0); popuWindow1.update (); popuWindow1.setOnDismissListener (new OnDismissListener () {// restore transparency in dismiss public void onDismiss () {WindowManager. layoutParams lp = getWindow (). getAttributes (); lp. alpha = 1f; getWindow (). setAttributes (lp );}});}}

2. 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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.popupwindowdemo.MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="23dp"        android:text="click to visit my csdn blog"        android:textColor="#1C86EE" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/textView2"        android:layout_centerHorizontal="true"        android:layout_marginTop="103dp"        android:text="click to show popupwindow" /></RelativeLayout>

3. popupwindow1.xml

<?xml version="1.0" encoding="UTF-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:background="@drawable/rounded_corners_bg"       android:orientation="vertical" >    <LinearLayout    android:layout_width="220dp"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:layout_gravity="center_vertical|center"    android:orientation="vertical" >        <TextView                android:id="@+id/group_menu1_passfile"                android:layout_width="fill_parent"                android:layout_height="40dp"                android:layout_marginLeft="20dp"                android:gravity="center_vertical"                android:text="item1"                android:textColor="#000000"                android:textSize="13sp" />            <View                android:layout_width="match_parent"                android:layout_height="0.5dp"                android:background="#999999" />            <TextView                android:id="@+id/group_menu1_playgame"                android:layout_width="fill_parent"                android:layout_height="40dp"                 android:layout_marginLeft="20dp"                android:gravity="center_vertical"                android:text="item2"                android:textColor="#000000"                android:textSize="13sp" />            <View                android:layout_width="match_parent"                android:layout_height="0.5dp"                android:background="#999999" />                        <TextView                android:id="@+id/group_menu1_removefriend"                android:layout_width="fill_parent"                android:layout_height="40dp"                android:layout_marginLeft="20dp"                android:gravity="center_vertical"                android:text="item3"                android:textColor="#000000"                android:textSize="13sp" />            <View                android:layout_width="match_parent"                android:layout_height="0.5dp"                android:background="#999999" />            <TextView                android:id="@+id/group_menu1_setremark"                android:layout_width="fill_parent"                android:layout_height="40dp"                android:layout_marginLeft="20dp"                android:gravity="center_vertical"                        android:text="item4"                android:textColor="#000000"                android:textSize="13sp" />            <View                android:layout_width="match_parent"                android:layout_height="0.5dp"                android:background="#999999" />                 <TextView                android:id="@+id/group_menu1_brokemate"                android:layout_width="fill_parent"                android:layout_height="40dp"                android:layout_marginLeft="20dp"                android:gravity="center_vertical"                        android:text="item5"                android:textColor="#000000"                android:textSize="13sp" />                     </LinearLayout></RelativeLayout>

4. rounded_corners_bg.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">     <solid android:color="#FFFFFF" />     <corners         android:topLeftRadius="5dp"        android:topRightRadius="5dp"         android:bottomRightRadius="5dp"        android:bottomLeftRadius="5dp"/> </shape>

Demo:

Http://download.csdn.net/detail/geeklei/7991521



PopupWindow for android Development

Based on what language? Html5, java? Please describe in detail.
 
Android developed the notebook App. you can insert images in the notebook to ask how to save such a diary in the database or

There are many ways. I personally think that images are stored on the SD card separately, and then the corresponding relationship between diaries and images is established through the database for storage. When used, they are queried from the database.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.