第8例:使用PopWindow实现菜单选择
2018-07-02 本文已影响74人
追梦小乐
核心思想知识点:
1)、PopWindow基本使用
效果图如下
GIF.gif
功能实现过程
1、popshow_anim.xml(PopWindow进入动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" >
</scale>
</set>
2、pophidden_anim.xml(PopWindow退出动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="0.0" >
</scale>
</set>
3、styles.xml(样式资源)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- 课程菜单栏 -->
<style name="PopupWindowAnimation">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
</style>
<style name="menu">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:textColor">#000000</item>
<item name="android:gravity">center</item>
<item name="android:textSize">16dp</item>
</style>
</resources>
4、pop_layout.xml(选择菜单布局)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="测试"
android:background="#ffffff"
android:textSize="30sp"
android:gravity="center"
android:layout_margin="5dp"/>
</RelativeLayout>
5、activity_main.xml(主界面布局)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="课程分类"
android:layout_centerInParent="true"
android:textColor="#000000"
android:textSize="22sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/select_1"
android:text="语言分类"
style="@style/menu" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="#000000"/>
<TextView
android:id="@+id/select_2"
android:text="类型"
style="@style/menu" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="#000000"/>
<TextView
android:id="@+id/select_3"
android:text="热门"
style="@style/menu" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="#000000"/>
<TextView
android:id="@+id/select_4"
android:text="难易"
style="@style/menu" />
</LinearLayout>
<ImageView
android:id="@+id/tag_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"
android:layout_alignParentBottom="true"/>
</LinearLayout>
</RelativeLayout>
6、MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = MainActivity.class.getSimpleName();
private PopupWindow popupWindow;
private ImageView tag_line;
private TextView txt;
private boolean isShow;
private int index;
private int oldIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化菜单项
findViewById(R.id.select_1).setOnClickListener(this);
findViewById(R.id.select_2).setOnClickListener(this);
findViewById(R.id.select_3).setOnClickListener(this);
findViewById(R.id.select_4).setOnClickListener(this);
tag_line = (ImageView) findViewById(R.id.tag_line);
}
@Override
public void onClick(View v) {
String str = null;
switch (v.getId()) {
case R.id.select_1://全部分类
str = "全部分类";
index = 1;
break;
case R.id.select_2://类型
str = "类型";
index = 2;
break;
case R.id.select_3://热门
str = "热门";
index = 3;
break;
case R.id.select_4://难易
str = "难易";
index = 4;
break;
}
setMenu(str);
}
/**
* 设置
*/
private void setMenu(String message) {
if (null == popupWindow){
View view = getLayoutInflater().inflate(R.layout.pop_layout, null);
txt = (TextView) view.findViewById(R.id.text);
//创建弹窗
popupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);//设置动画效果
popupWindow.setOutsideTouchable(false);//设置点击弹窗外部不关闭弹窗
popupWindow.setFocusable(false);//设置不获取焦点
}
Log.d(TAG,"isShow================"+isShow);
Log.d(TAG,"index================"+index);
Log.d(TAG,"oldIndex================"+oldIndex);
if (isShow){
if (index == oldIndex){
popupWindow.dismiss();//关闭弹窗
isShow = false;
return;
}
}
txt.setText(message);
popupWindow.showAsDropDown(tag_line);//设置显示弹窗
isShow = true;
oldIndex = index;
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow != null && popupWindow.isShowing()){
popupWindow.dismiss();
isShow = false;
}
}
});
}
}