欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)點擊某個按鈕指定位置彈出布局

 更新時間:2020年12月16日 11:26:57   作者:小小龍2  
這篇文章主要介紹了Android實現(xiàn)點擊某個按鈕指定位置彈出布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)點擊某個按鈕指定位置彈出布局,供大家參考,具體內(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) {
    
   }
  });
 }
 
 /**
  * 報表彈窗
  */
 private void showReportDialog(){
  reportList = new ArrayList<>();
  reportList.add("生產(chǎn)報表");
  reportList.add("設(shè)備報表");
  reportList.add("拋料率報表");
  reportList.add("在線預(yù)警報表");
  View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
 
  // 為了演示效果,簡單的設(shè)置了一些數(shù)據(jù),實際中大家自己設(shè)置數(shù)據(jù)即可,相信大家都會。
  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對象,指定寬度和高度
  PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  window.setWidth(tvReport.getWidth());
  // 設(shè)置動畫
//  window.setAnimationStyle(R.style.popup_window_anim);
  // 設(shè)置背景顏色
  window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
  // 設(shè)置可以獲取焦點
  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是因為我底部按鈕的高度是50
  lsvMore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if("生產(chǎn)報表".equals(reportName)){
 
    }
   }
  });
 }
 
 /**
  * 生產(chǎn)情況彈窗
  */
 private void showProductDialog(){
  productList = new ArrayList<>();
  productList.add("生產(chǎn)描述");
  productList.add("生產(chǎn)進度");
  productList.add("生產(chǎn)指標(biāo)");
  productList.add("異常信息");
  View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
 
  // 為了演示效果,簡單的設(shè)置了一些數(shù)據(jù),實際中大家自己設(shè)置數(shù)據(jù)即可,相信大家都會。
  ListView lsvMore = view.findViewById(R.id.lsvMore);
  lsvMore.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, productList));
 
  // 創(chuàng)建PopupWindow對象,指定寬度和高度
  PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  window.setWidth(tvProduct.getWidth());
  // 設(shè)置動畫
//  window.setAnimationStyle(R.style.popup_window_anim);
  // 設(shè)置背景顏色
  window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
  // 設(shè)置可以獲取焦點
  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);//獲取點擊的狀態(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="報表"/>
  <!--<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>

這里本來是想在右下角顯示一個小三角形的,不知道為啥不顯示,給它單獨拿出來設(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>

這是最終的效果。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android實現(xiàn)多線程下載文件(支持暫停、取消、斷點續(xù)傳)

    android實現(xiàn)多線程下載文件(支持暫停、取消、斷點續(xù)傳)

    本篇文章主要介紹了androids實現(xiàn)多線程下載文件,主要包括暫停、取消、斷點續(xù)傳等功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • 淺談Android Studio JNI生成so庫

    淺談Android Studio JNI生成so庫

    下面小編就為大家?guī)硪黄獪\談Android Studio JNI生成so庫。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android AS創(chuàng)建自定義布局案例詳解

    Android AS創(chuàng)建自定義布局案例詳解

    這篇文章主要介紹了Android AS創(chuàng)建自定義布局案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Android實現(xiàn)串口通信

    Android實現(xiàn)串口通信

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)串口通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • android創(chuàng)建和刪除文件夾和文件的實現(xiàn)方法

    android創(chuàng)建和刪除文件夾和文件的實現(xiàn)方法

    下面小編就為大家?guī)硪黄猘ndroid創(chuàng)建和刪除文件夾和文件的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • android 字體顏色選擇器(ColorPicker)介紹

    android 字體顏色選擇器(ColorPicker)介紹

    本文將詳細(xì)介紹android 字體顏色選擇器(ColorPicker)需要了解更多的朋友可以參考下
    2012-11-11
  • Android 中TabLayout自定義選擇背景滑塊的實例代碼

    Android 中TabLayout自定義選擇背景滑塊的實例代碼

    TabLayout是Android 的Material Design包中的一個控件,可以和V4包中的ViewPager搭配產(chǎn)生一個聯(lián)動的效果。接下來通過本文給大家分享TabLayout自定義選擇背景滑塊的實例代碼,感興趣的朋友一起學(xué)習(xí)吧
    2016-10-10
  • Android之Spinner用法詳解

    Android之Spinner用法詳解

    這篇文章主要介紹了Android之Spinner用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android彈幕框架 黑暗火焰使基本使用方法

    Android彈幕框架 黑暗火焰使基本使用方法

    這篇文章主要介紹了Android彈幕框架黑暗火焰使基本使用方法,需要的朋友可以參考下。今天我將分享由BiliBili開源的Android彈幕框架(DanmakuFlameMaster)的學(xué)習(xí)經(jīng)驗,感興趣的朋友一起看看吧
    2016-10-10
  • Android編程實現(xiàn)WebView全屏播放的方法(附源碼)

    Android編程實現(xiàn)WebView全屏播放的方法(附源碼)

    這篇文章主要介紹了Android編程實現(xiàn)WebView全屏播放的方法,結(jié)合實例形式較為詳細(xì)的分析了Android實現(xiàn)WebView全屏播放的布局與功能相關(guān)技巧,需要的朋友可以參考下
    2015-11-11

最新評論