Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局
本文實(shí)例為大家分享了Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局,供大家參考,具體內(nèi)容如下
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) {
}
});
}
/**
* 報(bào)表彈窗
*/
private void showReportDialog(){
reportList = new ArrayList<>();
reportList.add("生產(chǎn)報(bào)表");
reportList.add("設(shè)備報(bào)表");
reportList.add("拋料率報(bào)表");
reportList.add("在線預(yù)警報(bào)表");
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
// 為了演示效果,簡(jiǎn)單的設(shè)置了一些數(shù)據(jù),實(shí)際中大家自己設(shè)置數(shù)據(jù)即可,相信大家都會(huì)。
ListView lsvMore = (ListView) view.findViewById(R.id.lsvMore);
lsvMore.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, reportList));
// 創(chuàng)建PopupWindow對(duì)象,指定寬度和高度
PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
window.setWidth(tvReport.getWidth());
// 設(shè)置動(dòng)畫
// window.setAnimationStyle(R.style.popup_window_anim);
// 設(shè)置背景顏色
window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
// 設(shè)置可以獲取焦點(diǎn)
window.setFocusable(true);
// 設(shè)置可以觸摸彈出框以外的區(qū)域
window.setOutsideTouchable(true);
// 更新popupwindow的狀態(tài)
window.update();
// 以下拉的方式顯示,并且可以設(shè)置顯示的位置
// window.showAsDropDown(tvReport, 0, 20);
window.showAtLocation(tvReport, Gravity.LEFT | Gravity.BOTTOM, 0, 50);//這里的50是因?yàn)槲业撞堪粹o的高度是50
lsvMore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if("生產(chǎn)報(bào)表".equals(reportName)){
}
}
});
}
/**
* 生產(chǎn)情況彈窗
*/
private void showProductDialog(){
productList = new ArrayList<>();
productList.add("生產(chǎn)描述");
productList.add("生產(chǎn)進(jìn)度");
productList.add("生產(chǎn)指標(biāo)");
productList.add("異常信息");
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
// 為了演示效果,簡(jiǎn)單的設(shè)置了一些數(shù)據(jù),實(shí)際中大家自己設(shè)置數(shù)據(jù)即可,相信大家都會(huì)。
ListView lsvMore = view.findViewById(R.id.lsvMore);
lsvMore.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, productList));
// 創(chuàng)建PopupWindow對(duì)象,指定寬度和高度
PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
window.setWidth(tvProduct.getWidth());
// 設(shè)置動(dòng)畫
// window.setAnimationStyle(R.style.popup_window_anim);
// 設(shè)置背景顏色
window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
// 設(shè)置可以獲取焦點(diǎn)
window.setFocusable(true);
// 設(shè)置可以觸摸彈出框以外的區(qū)域
window.setOutsideTouchable(true);
// 更新popupwindow的狀態(tài)
window.update();
// 以下拉的方式顯示,并且可以設(shè)置顯示的位置
// 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);//獲取點(diǎn)擊的狀態(tài)名字
}
});
}
}
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="報(bào)表"/> <!--<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="生產(chǎn)情況"/> <!--<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="關(guān)于" 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>
這里本來是想在右下角顯示一個(gè)小三角形的,不知道為啥不顯示,給它單獨(dú)拿出來設(shè)置寬度和高度就顯示。希望有知道的給我解惑一下。大家知識(shí)共享。
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>
這是最終的效果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android點(diǎn)擊事件之多點(diǎn)觸摸與手勢(shì)識(shí)別的實(shí)現(xiàn)
- Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼
- Android實(shí)現(xiàn)自動(dòng)點(diǎn)擊無障礙服務(wù)功能的實(shí)例代碼
- Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊
- android實(shí)現(xiàn)點(diǎn)擊按鈕控制圖片切換
- Android實(shí)現(xiàn)圖片點(diǎn)擊放大
- android實(shí)現(xiàn)點(diǎn)擊圖片全屏展示效果
- Android Button按鈕點(diǎn)擊背景和文字變化操作
- Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
- 關(guān)于android連續(xù)點(diǎn)擊出現(xiàn)多個(gè)Activity界面的解決方法
- Android實(shí)現(xiàn)WebView點(diǎn)擊攔截跳轉(zhuǎn)原生
- Android擴(kuò)大View點(diǎn)擊范圍的方法
相關(guān)文章
Android 實(shí)現(xiàn)仿QQ拖拽氣泡效果的示例
這篇文章主要介紹了Android 實(shí)現(xiàn)仿QQ拖拽氣泡效果的示例,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Kotlin Option與Either及Result實(shí)現(xiàn)異常處理詳解
Kotlin異常處理,異常是在程序運(yùn)行時(shí)可能發(fā)生的不必要的問題,并突然終止您的程序。異常處理是一個(gè)過程,使用它可以防止程序出現(xiàn)可能破壞我們代碼的異常2022-12-12
android 網(wǎng)絡(luò)請(qǐng)求庫volley方法詳解
這篇文章主要介紹了android 網(wǎng)絡(luò)請(qǐng)求庫volley方法詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android Studio設(shè)置主題與字體大小圖文教程
這篇文章通過圖文詳細(xì)的給大家介紹了Android Studio中如何設(shè)置主題與字體大小,文章介紹的非常詳細(xì),相信對(duì)大家學(xué)習(xí)使用Android Studio具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2016-10-10
Android基于ViewPager實(shí)現(xiàn)類似微信頁面切換效果
這篇文章主要介紹了Android基于ViewPager實(shí)現(xiàn)類似微信頁面切換效果,通過Fragment適配器實(shí)現(xiàn)頁面切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

