亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PopupWindow使用方法詳解

 更新時(shí)間:2017年08月30日 10:10:05   作者:An_nAl  
這篇文章主要為大家詳細(xì)介紹了PopupWindow的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

學(xué)習(xí)了Android PopupWindow的使用技巧 【Android UI設(shè)計(jì)與開發(fā)】7.底部菜單欄(四)PopupWindow 實(shí)現(xiàn)顯示仿騰訊新聞底部彈出菜單,然后自己進(jìn)行了一下研究,寫一個(gè)總結(jié),方便以后學(xué)習(xí)。

效果圖:


1.PopupWindow的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:background="@color/colorAccent"
  android:gravity="center"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <TextView
    android:id="@+id/tv_popup_text"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:text="我就是彈窗"
    android:textSize="25sp"
    android:textColor="#ffffffff"
    android:layout_centerInParent="true"
    android:gravity="center"/>

</LinearLayout>

2.在res下新建anim文件夾,為窗口彈出消失寫動(dòng)畫:

popupwindow_in:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="250"
    android:fromYDelta="100.0%"
    android:toYDelta="0.0" />
</set>

popupwindow_out:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="250"
    android:fromYDelta="0.0"
    android:toYDelta="100%" />
</set>

添加style:

 <style name="anim_popup_window">
    <item name="android:windowEnterAnimation">@anim/popupwindow_in</item>
    <item name="android:windowExitAnimation">@anim/popupwindow_out</item>
  </style>

3.主界面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
  android:gravity="center"
  android:orientation="vertical"
  android:id="@+id/layout_home"
  android:background="#FFB5C5"
  tools:context="com.lotus.popupwindowdemo.HomeActivity">

  <TextView
    android:id="@+id/tv_show_popup_window"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="點(diǎn)擊顯示PopupWindow" />
</LinearLayout>

4.主界面代碼:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

  private LinearLayout layout_home;
  private TextView tv_show_popup_window;
  private PopupWindow mPopupWindow;
  private TextView tv_popup_text;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    // 引入窗口配置文件:即彈窗的界面
    View popupView = getLayoutInflater().inflate( R.layout.layout_popupwindow, null);
    popupView.setOnClickListener( this);

    tv_popup_text = (TextView) popupView.findViewById(R.id.tv_popup_text);
    tv_popup_text.setOnClickListener(this);

    // PopupWindow實(shí)例化
    mPopupWindow = new PopupWindow( popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
    // 設(shè)置PopupWindow是否可觸摸(設(shè)置為不可觸摸,那彈出框內(nèi)的任何控件都不能進(jìn)行任何點(diǎn)擊等等類似操作)
    mPopupWindow.setTouchable( true);
    // 設(shè)置非PopupWindow區(qū)域是否可觸摸
    // 1.若設(shè)置PopupWindow獲得焦點(diǎn)和非PopupWindow區(qū)域可觸摸,但實(shí)際上非PopupWindow區(qū)域的控件并不能響應(yīng)點(diǎn)擊事件等等
    // 2.若設(shè)置PopupWindow不可獲得焦點(diǎn),則不管非PopupWindow區(qū)域被設(shè)置能否觸摸,實(shí)際上非PopupWindow區(qū)域的控件都能響應(yīng)點(diǎn)擊事件等等
    // 3.若設(shè)置PopupWindow不可獲得焦點(diǎn),非PopupWindow區(qū)域被設(shè)置能觸摸,當(dāng)點(diǎn)擊非PopupWindow區(qū)域時(shí)能隱藏PopupWindow,而點(diǎn)擊返回鍵并不能隱藏窗口,
    //  此時(shí)通過按鈕只能控制窗口的彈出,并不能控制消失,消失只能通過點(diǎn)擊其他非PopupWindow區(qū)域
    mPopupWindow.setOutsideTouchable( false);
    // 如果不設(shè)置PopupWindow的背景,無論是點(diǎn)擊外部區(qū)域還是Back鍵都無法dismiss彈框(但目前并沒有發(fā)現(xiàn)此問題)
//    mPopupWindow.setBackgroundDrawable( new BitmapDrawable( getResources(), (Bitmap) null));
    // 設(shè)置PopupWindow顯示和隱藏時(shí)的動(dòng)畫
    mPopupWindow.setAnimationStyle(R.style.anim_popup_window);
    // 設(shè)置PopupWindow是否可獲得焦點(diǎn)
    // 1.如果設(shè)置為可獲得焦點(diǎn),不管非PopupWindow區(qū)域被設(shè)置能否觸摸,也會(huì)在點(diǎn)擊屏幕非PopupWindow區(qū)域和點(diǎn)擊返回鍵時(shí),使PopupWindow隱藏
    // 2.相反,如果設(shè)置為不可獲得焦點(diǎn),在點(diǎn)擊屏幕非PopupWindow區(qū)域或點(diǎn)擊返回鍵時(shí),都不能使PopupWindow隱藏
    mPopupWindow.setFocusable(false);


    layout_home = (LinearLayout) this.findViewById(R.id.layout_home);
    tv_show_popup_window = (TextView) this.findViewById( R.id.tv_show_popup_window);
    tv_show_popup_window.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if ( mPopupWindow.isShowing()) {
          // 隱藏窗口,如果設(shè)置了點(diǎn)擊窗口外消失,則不需要此方式隱藏
          mPopupWindow.dismiss();
          tv_show_popup_window.setText("點(diǎn)擊顯示PopupWindow");
        } else {
          // 彈出窗口顯示內(nèi)容視圖,默認(rèn)以錨定視圖的左下角為起點(diǎn),這里為點(diǎn)擊按鈕
//        mPopupWindow.showAsDropDown( view);//默認(rèn)在view(tv_show_popup_window)的下方出現(xiàn)
          mPopupWindow.showAtLocation( layout_home, Gravity.BOTTOM, 0, 0);
          tv_show_popup_window.setText("點(diǎn)擊使PopupWindow消失");

        }
      }
    });
  }

  @Override
  public void onClick(View view) {
    switch ( view.getId()){
      case R.id.tv_popup_text:
        Toast.makeText( getApplicationContext(),"我是PopupWindow內(nèi)的一個(gè)控件",Toast.LENGTH_SHORT).show();
        break;
    }
  }
}

注:分析屬性時(shí),注釋寫得有點(diǎn)多,因?yàn)榘l(fā)現(xiàn)屬性彼此間聯(lián)系緊密,所以要小心使用才行。

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

相關(guān)文章

  • android自定義等級(jí)評(píng)分圓形進(jìn)度條

    android自定義等級(jí)評(píng)分圓形進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了android自定義等級(jí)評(píng)分圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例

    Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例

    這篇文章主要介紹了Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-03-03
  • MediaPlayer音頻與視頻播放方法示例介紹

    MediaPlayer音頻與視頻播放方法示例介紹

    這篇文章主要為大家介紹了MediaPlayer音頻與視頻播放方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 給Android初學(xué)者的Gradle知識(shí)普及

    給Android初學(xué)者的Gradle知識(shí)普及

    剛學(xué) Android 不久,對(duì) Gradle 不懂,看了很多資料依然一知半解,很多人都這樣覺得,表示同感,下面小編來給大家講講 Gradle相關(guān)知識(shí),需要的朋友跟隨小編一起來學(xué)習(xí)一下
    2018-09-09
  • Android精確測(cè)量文本寬高及基線位置的方法

    Android精確測(cè)量文本寬高及基線位置的方法

    這篇文章主要給大家介紹了關(guān)于Android精確測(cè)量文本寬高及基線位置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 解決Android BitmapFactory的基本使用問題

    解決Android BitmapFactory的基本使用問題

    很多朋友給小編反饋使用方法BitmapFactory.decodeFile轉(zhuǎn)化Bitmap時(shí)報(bào)錯(cuò),究竟是什么原因?qū)е洛e(cuò)誤問題呢?今天通過本文給大家介紹下解決Android BitmapFactory的基本使用問題,感興趣的朋友一起看看吧
    2021-10-10
  • Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼

    Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼

    Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼,需要的朋友可以參考一下
    2013-05-05
  • 詳解Android中Intent對(duì)象與Intent Filter過濾匹配過程

    詳解Android中Intent對(duì)象與Intent Filter過濾匹配過程

    這篇文章主要介紹了Android中Intent對(duì)象與Intent Filter過濾匹配過程,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android Studio中Run按鈕是灰色的快速解決方法

    Android Studio中Run按鈕是灰色的快速解決方法

    這篇文章主要介紹了Android Studio中Run按鈕是灰色的快速解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-03-03
  • Android AOP基本用法全面詳解

    Android AOP基本用法全面詳解

    這篇文章主要為大家介紹了Android AOP基本用法全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09

最新評(píng)論