最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android用PopupWindow實現(xiàn)自定義Dailog

 更新時間:2017年01月21日 10:46:19   作者:風(fēng)繼續(xù)吹_z  
這篇文章主要為大家詳細介紹了Android用PopupWindow實現(xiàn)自定義Dailog的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android的PopupWindow是個很有用的widget,利用它可以實現(xiàn)懸浮窗體的效果,比如實現(xiàn)一個懸浮的菜單,最常見的應(yīng)用就是在視頻播放界面里,做一個工具欄,用來控制播放進度。本文利用PopupWindow來實現(xiàn)一個通用的Dailog,類似Android系統(tǒng)的AlertDailog,從中學(xué)習(xí)和掌握有關(guān)PopupWindow和Dailog的使用和實現(xiàn)細節(jié)。

界面效果如圖所示,點擊 Click 按鈕后,彈出對話框提示。


(1).  CustomDailog的布局

首先定義 CustDailog的布局文件,由系統(tǒng)的AlertDailog可以知道,一個對話框包含了三個要素,一個是Title,即標(biāo)題,一個是Message,即主體內(nèi)容,還有一個是Button,即確定和取消的按鈕,用來與用戶交互。因此,布局設(shè)計如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:background="@drawable/shape_bg"
  android:layout_margin="10dp">
                                                                                                                                                         
  <TextView   
    android:id="@+id/CustomDlgTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_margin="10dp"
    android:gravity="center"/>
                                                                                                                                                           
  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>
                                                                                                                                                           
  <LinearLayout
    android:id="@+id/CustomDlgContentView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="5dp" />
                                                                                                                                                         
  <TextView
    android:id="@+id/CustomDlgContentText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:layout_margin="5dp"
    android:paddingLeft="5sp"/>
                                                                                                                                                       
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="5dp" >
                                                                                                                                                         
    <Button
      android:id="@+id/CustomDlgButtonOK"
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:layout_height="wrap_content"
      android:visibility="gone"/>
                                                                                                                                                             
    <Button
      android:id="@+id/CustomDlgButtonCancel"
      android:layout_width="0dp"
      android:layout_weight="0.5"
      android:layout_height="wrap_content"     
      android:visibility="gone"/>
                                                                                                                                                      
  </LinearLayout>
                                                                                                                                                       
</LinearLayout>

其中,shap_bg.xml 是Dailog的背景的定義文件,你可以修改此文件,來改變Dailog的背景:

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
 xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#e6ecee" />
  <stroke android:width="1.0dip" android:color="@android:color/darker_gray" />
  <corners android:radius="8.0dip" />
</shape>

(2). CustomDailog的定義

CustomDailog的接口,可以類比AlertDailg的接口定義,主要包括如下一些方法:

1.  setTitle 設(shè)置標(biāo)題
2.  setMessage 設(shè)置主體內(nèi)容
3.  setPositiveButton 設(shè)置 “確定” 按鈕
4.  setNegativeButton 設(shè)置 “取消” 按鈕
5.  show   顯示
6.  dimiss 消失

其定義如下:

package com.ticktick.popdailog;
                                                                         
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
                                                                           
public class CustomDailog {
                                                                        
  private View mParent;
  private PopupWindow mPopupWindow;
  private LinearLayout mRootLayout; 
  private LayoutParams mLayoutParams; 
                                                                        
  //PopupWindow必須有一個ParentView,所以必須添加這個參數(shù)
  public CustomDailog(Context context, View parent) {
                                                                          
    mParent = parent;
                                                                          
    LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
                                                                          
    //加載布局文件
    mRootLayout = (LinearLayout)mInflater.inflate(R.layout.custom_dailog, null); 
                                                                              
    mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  } 
                                                                        
  //設(shè)置Dailog的標(biāo)題
  public void setTitle(String title) {
    TextView mTitle = (TextView)mRootLayout.findViewById(R.id.CustomDlgTitle);
    mTitle.setText(title);
  }
                                                                        
  //設(shè)置Dailog的主體內(nèi)容
  public void setMessage(String message) {
    TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText);
    mMessage.setText(message);
  }
                                                                        
  //設(shè)置Dailog的“確定”按鈕
  public void setPositiveButton(String text,OnClickListener listener ) {
    final Button buttonOK = (Button)mRootLayout.findViewById(R.id.CustomDlgButtonOK);
    buttonOK.setText(text);
    buttonOK.setOnClickListener(listener);
    buttonOK.setVisibility(View.VISIBLE);
  }
                                                                        
  //設(shè)置Dailog的“取消”按鈕
  public void setNegativeButton(String text,OnClickListener listener ) {
    final Button buttonCancel = (Button)mRootLayout.findViewById(R.id.CustomDlgButtonCancel);
    buttonCancel.setText(text);
    buttonCancel.setOnClickListener(listener);
    buttonCancel.setVisibility(View.VISIBLE);
  }
                                                                        
  //替換Dailog的“主體”布局
  public void setContentLayout(View layout) {
                                                                          
    TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText);
    mMessage.setVisibility(View.GONE);
                                                                          
    LinearLayout contentLayout = (LinearLayout)mRootLayout.findViewById(R.id.CustomDlgContentView);   
    contentLayout.addView(layout);       
  }
                                                                        
  //設(shè)置Dailog的長寬
  public void setLayoutParams(int width, int height) {
    mLayoutParams.width = width;
    mLayoutParams.height = height;
  }
                                                                        
  //顯示Dailog
  public void show() {
                                                                        
    if(mPopupWindow == null) {
      mPopupWindow = new PopupWindow(mRootLayout, mLayoutParams.width,mLayoutParams.height);
      mPopupWindow.setFocusable(true);
    }
                                                                          
    mPopupWindow.showAtLocation(mParent, Gravity.CENTER, Gravity.CENTER, Gravity.CENTER);
  }
                                                                        
  //取消Dailog的顯示
  public void dismiss() {
                                                                          
    if(mPopupWindow == null) {
      return;
    }
                                                                          
    mPopupWindow.dismiss();
  }
}

(3). 在Activity中的使用方法

由于 PopupWindow 的顯示必須給一個ParentView,在Activity中使用的話,最簡單的方法就是將整個activity的“根View”傳遞給這個PopupWindow,這樣就可以在整個屏幕的正中央來顯示Dailog,獲取Acitivity的根View的方法如下:

findViewById(android.R.id.content)).getChildAt(0);

因此,上面定義的 CunstomDailog的使用方法如下所示:

final CustomDailog dailog = new CustomDailog(this,getRootLayout());
dailog.setTitle("Warning");
dailog.setMessage("This is ticktick's blog!");
dailog.setPositiveButton("OK", new OnClickListener() {    
  @Override
  public void onClick(View v) {
    dailog.dismiss();     
  }
});
dailog.setNegativeButton("Cancel", new OnClickListener() {    
  @Override
  public void onClick(View v) {
  dailog.dismiss();     
  }
});
dailog.show();

到此為止,整個Dailog的實現(xiàn)就介紹到這里了。

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

相關(guān)文章

  • Android ProgressBar實現(xiàn)進度條效果

    Android ProgressBar實現(xiàn)進度條效果

    這篇文章主要為大家詳細介紹了Android ProgressBar實現(xiàn)進度條效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android Studio 運行按鈕灰色的完美解決方法

    Android Studio 運行按鈕灰色的完美解決方法

    今天新建項目的時候突然發(fā)現(xiàn)編譯后運行按鈕為灰色,今天小編給大家?guī)砹薃ndroid Studio 運行按鈕灰色的完美解決方法,非常不錯,對大家的需要或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-10-10
  • Android Fragment動態(tài)創(chuàng)建詳解及示例代碼

    Android Fragment動態(tài)創(chuàng)建詳解及示例代碼

    這篇文章主要介紹了Android Fragment動態(tài)創(chuàng)建詳解的相關(guān)資料,并附實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下
    2016-11-11
  • Android實例HandlerThread源碼分析

    Android實例HandlerThread源碼分析

    本篇文章主要給大家通過實例代碼分析了Android中HandlerThread的用法以及步驟,需要的朋友參考學(xué)習(xí)下吧。
    2017-12-12
  • Android加載View中Background詳解

    Android加載View中Background詳解

    本文講解的是Android什么時候進行View中Background的加載,十分的詳盡,十分全面細致,附上所有代碼,這里推薦給大家,希望大家能夠喜歡。
    2015-03-03
  • Android RenderScript高斯模糊

    Android RenderScript高斯模糊

    這篇文章主要介紹了Android RenderScript高斯模糊的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android和JavaScript相互調(diào)用的方法

    Android和JavaScript相互調(diào)用的方法

    這篇文章主要介紹了Android和JavaScript相互調(diào)用的方法,實例分析了Android的WebView執(zhí)行JavaScript及JavaScript訪問Android的技巧,需要的朋友可以參考下
    2015-12-12
  • Android Notification實現(xiàn)動態(tài)顯示通話時間

    Android Notification實現(xiàn)動態(tài)顯示通話時間

    這篇文章主要為大家詳細介紹了Android Notification實現(xiàn)動態(tài)顯示通話時間,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android將項目導(dǎo)出為Library并在項目中使用教程

    Android將項目導(dǎo)出為Library并在項目中使用教程

    這篇文章主要介紹了Android將項目導(dǎo)出為Library并在項目中使用教程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Flutter源碼分析之自定義控件(RenderBox)指南

    Flutter源碼分析之自定義控件(RenderBox)指南

    寫了兩天的flutter,發(fā)現(xiàn)控件樣式很多,flutter資源很少,本文在于實用性,可以減少頁面代碼,下面這篇文章主要介紹了Flutter源碼分析之自定義控件(RenderBox)的相關(guān)資料,需要的朋友可以參考下
    2021-08-08

最新評論

云龙县| 芒康县| 新泰市| 会东县| 柘城县| 绍兴市| 临洮县| 准格尔旗| 中山市| 平陆县| 习水县| 子长县| 临西县| 新民市| 马公市| 图片| 武隆县| 奇台县| 江华| 加查县| 天峨县| 枣庄市| 同江市| 宜章县| 平邑县| 万载县| 洪雅县| 平江县| 密山市| 新和县| 新密市| 清原| 郎溪县| 元朗区| 陆丰市| 乌兰察布市| 湘西| 菏泽市| 淮滨县| 威海市| 华安县|