Android实现点击某个按钮指定位置弹出布局
作者:小小龍2 发布时间:2023-04-10 16:09:59
标签:Android,按钮,弹出布局
本文实例为大家分享了Android实现点击某个按钮指定位置弹出布局,供大家参考,具体内容如下
package com.topcee.report.report;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.topcee.report.R;
import java.util.ArrayList;
import java.util.List;
public class HomeActivity extends Activity {
private Context context;
private List<String> reportList;
private List<String> productList;
private TextView tvReport;
private TextView tvProduct;
private TextView tvCompany;
private String reportName = "";
private String productName = "";
private String companyName = "";
private ListView lvData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
context = HomeActivity.this;
initView();
}
private void initView(){
lvData = findViewById(R.id.lv_data);
lvData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
tvReport = findViewById(R.id.tv_report);
tvProduct = findViewById(R.id.tv_product);
tvCompany = findViewById(R.id.tv_company);
tvReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showReportDialog();
}
});
tvProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showProductDialog();
}
});
tvCompany.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
/**
* 报表弹窗
*/
private void showReportDialog(){
reportList = new ArrayList<>();
reportList.add("生产报表");
reportList.add("设备报表");
reportList.add("抛料率报表");
reportList.add("在线预警报表");
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
// 为了演示效果,简单的设置了一些数据,实际中大家自己设置数据即可,相信大家都会。
ListView lsvMore = (ListView) view.findViewById(R.id.lsvMore);
lsvMore.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, reportList));
// 创建PopupWindow对象,指定宽度和高度
PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
window.setWidth(tvReport.getWidth());
// 设置动画
// window.setAnimationStyle(R.style.popup_window_anim);
// 设置背景颜色
window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
// 设置可以获取焦点
window.setFocusable(true);
// 设置可以触摸弹出框以外的区域
window.setOutsideTouchable(true);
// 更新popupwindow的状态
window.update();
// 以下拉的方式显示,并且可以设置显示的位置
// window.showAsDropDown(tvReport, 0, 20);
window.showAtLocation(tvReport, Gravity.LEFT | Gravity.BOTTOM, 0, 50);//这里的50是因为我底部按钮的高度是50
lsvMore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if("生产报表".equals(reportName)){
}
}
});
}
/**
* 生产情况弹窗
*/
private void showProductDialog(){
productList = new ArrayList<>();
productList.add("生产描述");
productList.add("生产进度");
productList.add("生产指标");
productList.add("异常信息");
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
// 为了演示效果,简单的设置了一些数据,实际中大家自己设置数据即可,相信大家都会。
ListView lsvMore = view.findViewById(R.id.lsvMore);
lsvMore.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, productList));
// 创建PopupWindow对象,指定宽度和高度
PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
window.setWidth(tvProduct.getWidth());
// 设置动画
// window.setAnimationStyle(R.style.popup_window_anim);
// 设置背景颜色
window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
// 设置可以获取焦点
window.setFocusable(true);
// 设置可以触摸弹出框以外的区域
window.setOutsideTouchable(true);
// 更新popupwindow的状态
window.update();
// 以下拉的方式显示,并且可以设置显示的位置
// window.showAsDropDown(tvProduct, 0, 20);
window.showAtLocation(tvProduct, Gravity.CENTER | Gravity.BOTTOM, 0, 50);
lsvMore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
productName = productList.get(position);//获取点击的状态名字
}
});
}
}
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".report.HomeActivity">
<LinearLayout
android:id="@+id/ll_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/activity_home_btn_layout">
<ListView
android:id="@+id/lv_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null">
</ListView>
</LinearLayout>
<View
android:id="@+id/activity_home_bottom_line_layout"
android:layout_above="@+id/activity_home_btn_layout"
style="@style/style_row_line_view"/>
<LinearLayout
android:id="@+id/activity_home_btn_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/tv_report"
style="@style/style_activity_home_text_view"
android:layout_weight="1"
android:clickable="true"
android:background="@drawable/btn_pressed"
android:text="报表"/>
<!--<ImageView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginBottom="3dp"
android:clickable="true"
android:background="@drawable/btn_pressed"/>-->
<View style="@style/style_column_line_view"/>
<TextView
android:id="@+id/tv_product"
style="@style/style_activity_home_text_view"
android:layout_weight="1"
android:clickable="true"
android:background="@drawable/btn_pressed"
android:text="生产情况"/>
<!--<ImageView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginBottom="3dp"
android:clickable="true"
android:background="@drawable/btn_pressed"/>-->
<View style="@style/style_column_line_view"/>
<TextView
android:id="@+id/tv_company"
style="@style/style_activity_home_text_view"
android:layout_weight="1"
android:text="关于"
android:clickable="true"
android:background="@drawable/btn_pressed"/>
<!--<ImageView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginBottom="3dp"
android:clickable="true"
android:background="@drawable/btn_pressed"/>-->
</LinearLayout>
</RelativeLayout>
btn_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/triangle_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/triangle_bg"></item>
</selector>
triangle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/shape"
android:left="-2dp"
android:bottom="-2dp">
<!-- 长方形 -->
<shape android:shape="rectangle">
<stroke android:color="#DFDFDF" android:width="1dp"/>
<solid android:color="#DFDFDF"></solid>
</shape>
</item>
<item android:id="@+id/shape_id">
<!-- 正三角 -->
<rotate
android:fromDegrees="50"
android:toDegrees="-50"
android:pivotX="-50%"
android:pivotY="100%">
<shape android:shape="rectangle">
<solid android:color="#DFDFDF"/>
</shape>
</rotate>
</item>
</layer-list>
triangle_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/shape"
android:left="-2dp"
android:bottom="-2dp">
<!-- 长方形 -->
<shape android:shape="rectangle">
<stroke android:color="#DFDFDF" android:width="1dp"/>
<solid android:color="#DFDFDF"></solid>
</shape>
</item>
<item android:id="@+id/shape_id">
<!-- 正三角 -->
<rotate
android:fromDegrees="50"
android:toDegrees="-50"
android:pivotX="-50%"
android:pivotY="100%">
<shape android:shape="rectangle">
<solid android:color="#DFDFDF"/>
</shape>
</rotate>
</item>
</layer-list>
这里本来是想在右下角显示一个小三角形的,不知道为啥不显示,给它单独拿出来设置宽度和高度就显示。希望有知道的给我解惑一下。大家知识共享。
popupwindow.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:background="@drawable/popupwindow_bg">
<ListView
android:id="@+id/lsvMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
这是最终的效果。
来源:https://blog.csdn.net/u014095878/article/details/80094409


猜你喜欢
- 方式1. 使用HashtableMap<String,Object> hashtable=new Hashtable
- 现在版本更新有两种处理方式:跳转到App应用市场,通过应用市场下载更新安装。在App内进行Apk下载,下载完成后更新安装。实现思路:请求后台
- 概述MerkleTree被广泛的应用在比特币技术中,本文旨在通过代码实现一个简单的MerkleTree,并计算出Merkle tree的 T
- 讲解之前需要说明的是旋转屏幕:在系统的自动旋转屏幕开启的情况下,我们旋转屏幕手动设置屏幕:我们自己去调用Activity的 setReque
- 在application.xml加上以下配置mybatis-plus.configuration.map-underscore-to-cam
- 简介Lambda表达式是Java SE 8中一个重要的新特性。lambda表达式允许你通过表达式来代替功能接口。 lambda表达式就和方法
- C# 关于Invoke首先说下,invoke和begininvoke的使用有两种情况:control中的invoke、begininvoke
- 窗体展示开始后展示结束摇色展示代码导入的命名空间using System;using System.Collections.Generic;
- /// <summary>/// 获取字符串最长的数字/// </summary>/// <param nam
- 介绍和使用场景1)什么是消息一个事件,需要广播或者单独传递给某个接口2)为什么使用这个配置更新了,但是其他系统不知道是否更新SpringCl
- 基本概念Spring Validation 验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范
- 单例模式动机有时候只有一个类的实例是很重要的。比如,一个系统应该只有一个窗口管理实例。单例模式是最简单设计模式:类负责实例化自己,确保只有一
- 简介mutable(可变)和immutable(不可变)对象是我们在java程序编写的过程中经常会使用到的。可变类型对象就是说,对象在创建之
- 1 什么是class对象类是程序的一部分,每个类都有一个class对象。换言之,每当编写并且编译了一个新类,就会产生一个class对象(更恰
- C#控制台程序使用Log4net日志组件,供大家参考,具体内容如下1、Log4net一般都不陌生,但是在配置上不同类型的项目又不相同的地方比
- SpringBoot 配置SwaggerUI 访问404的小坑。在学习SpringBoot构建Restful API的时候遇到了一个小坑,配
- 使用TransitionDrawable渐变切换多张图片,供大家参考,具体内容如下1、定义变量private int change = 0;
- 先给大家这是下效果图:谷歌提供的v4包,ViewPager在布局文件中,先添加<android.support.v4.view.Vie
- 不得不说opencv是个强大的东东,以前做一个项目的一个模块时使用到进行图形处理,这次是想将一个视频的播放放慢,以前在网上看到opencv有
- 教程展示了如何在Spring应用程序中使用GenericApplicationContext 。在该示例中,我们创建了一个Spring Bo