Android(安卓)popupwindow 示例程序一
16lz
2021-01-25
经过多番测试实践,实现了popupwindow 弹出在指定控件的下方。代码上有注释,有需要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。接下来看代码实现。
项目资源下载:点击这里
TestPopwindow2.class
package com.example.popwindowdemo;import android.app.ActionBar.LayoutParams;import android.app.Activity;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.view.LayoutInflater;import android.view.View;import android.widget.PopupWindow;public class TestPopwindow2 extends PopupWindow {// 根视图private View mRootView;// LayoutInflaterLayoutInflater mInflater;public TestPopwindow2(Activity context) {super(context);mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);mRootView = mInflater.inflate(R.layout.test_popwindow_2, null);setContentView(mRootView);this.setWidth(LayoutParams.WRAP_CONTENT);this.setHeight(LayoutParams.WRAP_CONTENT);// 设置PopUpWindow弹出的相关属性setTouchable(true);setOutsideTouchable(true);setFocusable(true);setBackgroundDrawable(new BitmapDrawable(context.getResources()));update();getContentView().setFocusableInTouchMode(true);getContentView().setFocusable(true);setAnimationStyle(R.style.AppBaseTheme);}}MainActivity.class
package com.example.popwindowdemo;import android.os.Bundle;import android.app.Activity;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.PopupWindow.OnDismissListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener,OnDismissListener {private TestPopwindow2 mTestPopwindow2 = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitUI();}private void InitUI() {// 实例化TestPopwindow2mTestPopwindow2 = new TestPopwindow2(this);// 设置点击其他位置mTestPopwindow2消失mTestPopwindow2.setOnDismissListener(this);Button buttonTest2 = (Button) findViewById(R.id.buttonTest2);buttonTest2.setOnClickListener(this);}private void OnPopwindowTest2() {if (mTestPopwindow2 == null)return;// location获得控件的位置int[] location = new int[2];View v = findViewById(R.id.buttonTest2);if (v != null)v.getLocationOnScreen(location); // 控件在屏幕的位置mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme);// mTestPopwindow2弹出在某控件(button)的下面mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT,location[0] - v.getWidth(), location[1] + v.getHeight());}// mTestPopwindow2布局控件的监听public void OnclickTestListener(View view) {switch (view.getId()) {case R.id.layoutSeclect1:Toast.makeText(this, "系统热门方案", Toast.LENGTH_SHORT).show();break;case R.id.layoutSeclect2:Toast.makeText(this, "个人热门方案", Toast.LENGTH_SHORT).show();break;default:break;}if (mTestPopwindow2 != null)mTestPopwindow2.dismiss();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.buttonTest2:OnPopwindowTest2();break;default:break;}}// 点击其他地方消失@Overridepublic void onDismiss() {}}test_popwindow_2.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="#0000" > <!-- popupwimdow的布局注意不能使用类似 android:layout_marginTop=""这样属性,特别是相对布局时候. 布局或者控件整体靠左等都会影响popupwimdow定位使用 --> <LinearLayout android:id="@+id/hot_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/title_function_bg" android:orientation="vertical" > <LinearLayout android:id="@+id/layoutSeclect1" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:onClick="OnclickTestListener" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="系统热门方案" android:textColor="#fff" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:id="@+id/layoutSeclect2" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:onClick="OnclickTestListener" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="个人热门方案" android:textColor="#fff" android:textSize="18sp" /> </LinearLayout> </LinearLayout></LinearLayout>activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayoutMain" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right"> <Button android:id="@+id/buttonTest2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试2" /> </LinearLayout></LinearLayout>效果图如下
项目资源下载:点击这里
转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
如有转载未注明出处博客网址,或者没有得到作者的同意。作者持有版权所有权。
更多相关文章
- android布局全屏显示,状态栏和导航栏透明设置
- 使用setContentView的方式更换布局文件从而更换界面
- Android(安卓)NavigationDrawer(侧滑导航栏 DrawerLayout + Navi
- Android(安卓)圆角图片CircleImageView
- android菜瓜笔记之android lint problem
- Android(安卓)ViewPager实例代码介绍2。
- android初中高开发工程师必需要装的一个APP
- android 布局背景模糊化处理
- Android(安卓)- ToDoList 详解