微信在日常的生活中运用得越来越广泛,今天我尝试着做出这样的界面,代码如下:
MainActivity.java
package com.whisker.custompopupwindow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button setButton;
private Button addButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setButton = (Button) findViewById(R.id.btnSet);
addButton = (Button) findViewById(R.id.btnAdd);
setButton.setOnClickListener(clickListener);
addButton.setOnClickListener(clickListener);
}
private OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSet:
MorePopWindow morePopWindow = new MorePopWindow(MainActivity.this);
morePopWindow.showPopupWindow(setButton);
break;
case R.id.btnAdd:
AddPopWindow addPopWindow = new AddPopWindow(MainActivity.this);
addPopWindow.showPopupWindow(addButton);
break;
}
}
};
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/animation_layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dip"
android:background="@drawable/abc_ab_bottom_solid_dark_holo">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="微信"
android:textColor="@color/lightgray"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnSearch"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dip"
android:visibility="gone"
android:background="@drawable/actionbar_search_icon"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dip"
android:background="@drawable/actionbar_add_icon"/>
<Button
android:id="@+id/btnSet"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/actionbar_more_icon"/>
</LinearLayout>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
package com.whisker.custompopupwindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MorePopWindow extends PopupWindow{
private View contentView;
public MorePopWindow(final Activity context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.more_popup_dialog, null);
int h = context.getWindowManager().getDefaultDisplay().getHeight();
int w = context.getWindowManager().getDefaultDisplay().getWidth();
this.setContentView(contentView);
this.setWidth(w/2 + 50);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.update();
ColorDrawable colorDrawable = new ColorDrawable(0000000000);
this.setBackgroundDrawable(colorDrawable);
this.setAnimationStyle(R.style.AnimationPreview);
LinearLayout albumLayout = (LinearLayout) contentView.findViewById(R.id.album);
LinearLayout setLayout = (LinearLayout) contentView.findViewById(R.id.set);
LinearLayout exitLayout = (LinearLayout) contentView.findViewById(R.id.exit);
LinearLayout cardLayout = (LinearLayout) contentView.findViewById(R.id.card);
LinearLayout saveLayout = (LinearLayout) contentView.findViewById(R.id.save);
albumLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "album");
MorePopWindow.this.dismiss();
}
});
setLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "set");
MorePopWindow.this.dismiss();
}
});
exitLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "exit");
MorePopWindow.this.dismiss();
}
});
saveLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "save");
MorePopWindow.this.dismiss();
}
});
cardLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "card");
MorePopWindow.this.dismiss();
}
});
}
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
} else {
this.dismiss();
}
}
}
AddPopWindow.java
package com.whisker.custompopupwindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class AddPopWindow extends PopupWindow{
private View contentView;
public AddPopWindow(final Activity context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.add_popup_dialog, null);
int w = context.getWindowManager().getDefaultDisplay().getWidth();
int h = context.getWindowManager().getDefaultDisplay().getHeight();
this.setContentView(contentView);
this.setWidth(w/2 + 50);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.update();
ColorDrawable colorDrawable = new ColorDrawable(0000000000);
this.setBackgroundDrawable(colorDrawable);
this.setAnimationStyle(R.style.AnimationPreview);
LinearLayout addTaskLayout = (LinearLayout) contentView.findViewById(R.id.add_task_layout);
LinearLayout teamMemberLayout = (LinearLayout) contentView.findViewById(R.id.team_member_layout);
addTaskLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "add");
//Toast.makeText(, "按了Add", Toast.LENGTH_SHORT).show();
AddPopWindow.this.dismiss();
}
});
teamMemberLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "team");
//Toast.makeText(getContentView(), "按了Team", Toast.LENGTH_SHORT).show();
AddPopWindow.this.dismiss();
}
});
}
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
} else {
this.dismiss();
}
}
}
add_popup_dialog.xml
package com.whisker.custompopupwindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class AddPopWindow extends PopupWindow{
private View contentView;
public AddPopWindow(final Activity context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.add_popup_dialog, null);
int w = context.getWindowManager().getDefaultDisplay().getWidth();
int h = context.getWindowManager().getDefaultDisplay().getHeight();
this.setContentView(contentView);
this.setWidth(w/2 + 50);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.update();
ColorDrawable colorDrawable = new ColorDrawable(0000000000);
this.setBackgroundDrawable(colorDrawable);
this.setAnimationStyle(R.style.AnimationPreview);
LinearLayout addTaskLayout = (LinearLayout) contentView.findViewById(R.id.add_task_layout);
LinearLayout teamMemberLayout = (LinearLayout) contentView.findViewById(R.id.team_member_layout);
addTaskLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "add");
//Toast.makeText(, "按了Add", Toast.LENGTH_SHORT).show();
AddPopWindow.this.dismiss();
}
});
teamMemberLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("whisker", "team");
//Toast.makeText(getContentView(), "按了Team", Toast.LENGTH_SHORT).show();
AddPopWindow.this.dismiss();
}
});
}
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
} else {
this.dismiss();
}
}
}
nore_popup_dialog.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:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@drawable/abc_ab_bottom_solid_dark_holo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/defalt_head"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_horizontal"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Whisker"
android:textColor="@color/white"
android:textSize="18sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:text="demiwhisker"
android:textColor="@color/white"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/group_divider"/>
<LinearLayout
android:id="@+id/album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="fitCenter"
android:src="@drawable/ofm_photo_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="我的相册"
android:textColor="@color/white"
android:textSize="18sp"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/group_divider"/>
<LinearLayout
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="fitCenter"
android:src="@drawable/ofm_collect_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="我的收藏"
android:textColor="@color/white"
android:textSize="18sp"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/group_divider"/>
<LinearLayout
android:id="@+id/card"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="fitCenter"
android:src="@drawable/ofm_card_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="我的银行卡"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/group_divider"/>
<LinearLayout
android:id="@+id/set"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="fitCenter"
android:src="@drawable/ofm_setting_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="设置"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/group_divider"/>
<LinearLayout
android:id="@+id/exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="fitCenter"
android:src="@drawable/ofm_blacklist_icon" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:background="@null"
android:gravity="center"
android:text="退出登录"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
style.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="AnimationPreview">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
</resources>
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
<scale
android:fromXScale="0.001"
android:toXScale="1.0"
android:fromYScale="0.001"
android:toYScale="1.0"
android:duration="200"
android:pivotX="100%"
android:pivotY="10%"/>
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
<scale
android:fromXScale="1.0"
android:toXScale="0.001"
android:fromYScale="1.0"
android:toYScale="0.001"
android:pivotX="100%"
android:pivotY="10%"
android:duration="200"/>
</set>
但是在开发过程中遇到了这样一个问题,我希望在点击各个项目的时候,触发Toast事件,但是第一个参数一直没有找到对的参数,希望以后能够解决。
效果图如下: