一.AlertDialog
1.實現(xiàn)方式
?AlertDialog?.?Builder?builder?=?new?AlertDialog?.?Builder?(?context?); | 構(gòu)建?Dialog?的各種參數(shù) |
?Builder?.?setlcon?(?int?iconld?); | 添加?ICON? |
?Builder?.?setTitle?(?CharSequence?title?); | 添加標題 |
?Builder?.?setMessage?(?CharSequence?message?); | 添加消息 |
?Builder?.?setView?(?View?view?); | 設(shè)置自定義布局 |
?Builder?.?create?(); | 創(chuàng)建?Dialog? |
?Builder?.?show?(); | 顯示對話框 |
?setPositiveButton? | 確定按鈕 |
?setNegativeButton? | 取消按鈕 |
?setNeutralButton? | 中間按鈕 |
1.1注意細節(jié)寫法
public void leoClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("我是IKUN")
.setMessage("你干嘛~~~")
.create()
.show();
}
后兩項create和show必須放后面前三項可以任意調(diào)換位置,
create返回的是AlertDialog show在AlertDialog 中
在activity_main.xml中寫下:
<Button
android:text="坤坤集合"
android:onClick="leoClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
顯示效果如圖:
?1.2設(shè)置下部三個按鈕
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNeutralButton("中間", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
效果如圖所示:
?排布根據(jù)不同的型號,是不同的
1,3自定義布局設(shè)置.setView(dialogView)樣式
View dialogView = getLayoutInflater().inflate(R.layout.dialog_view, null);.setView(dialogView)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="你干嘛~~雞你太美"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
設(shè)置后效果如下:
?二.控件PopupWindow
1.常用方法
1.?setContentView?(?View?contentView?): | 設(shè)置?PopupWindow?顯示的?View |
2.?showAsDropDown?(?View?anchor?): | 相對某個控件的位置(正左下方),無偏移 |
3.showAsDropDown(?View?anchor?,?int?xoff?,?int?yoff?): | 相對某個控件的位置,有偏移 |
4.?setFocusable?(?boolean?focusable?) | 設(shè)置是否獲取焦點 |
5.?setBackgroundDrawable?(?Drawable?background?) | 設(shè)置背景 |
6.?dismiss?() | 關(guān)閉彈窗 |
7.?setAnimationStyle?(?int?animationStyle?) | 設(shè)置加載動畫 |
8.?setTouchable?(?boolean?touchable?) | 設(shè)置觸摸使能 |
9.?setOutsideTouchable?(?boolean?touchable?) | 設(shè)置?PopupWindow?外面的觸摸使能 |
2.popupWindow中提供很多簡便構(gòu)造方法
無參構(gòu)造
?一個參數(shù)的contentView
兩個參數(shù)的寬和高width,height?
?三個參數(shù)的 寬,高width,height 和contentView
?四個參數(shù)寬,高width,height ,contentView和focusable
一般都是用三參或者四參的
完整代碼:
<Button
android:text="彈出PopupWindow"
android:onClick="leoClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
2.1contentView就是彈窗顯示布局
?此處寫300是可以的,但是為了讓popupWindow剛好包裹住popupView,一般通過以下方式
?完整代碼:
public void leoClick(View view) {
View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(view);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@mipmap/ic_launcher"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="168dp"
android:layout_height="94dp"
android:padding="5dp"
android:text="廣西"
android:textSize="18sp" />
<Button
android:id="@+id/btn2"
android:layout_width="168dp"
android:layout_height="94dp"
android:padding="5dp"
android:text="欽州"
android:textSize="18sp" />
</LinearLayout>
效果如圖:
也可以對兩個按鈕進行偏移
3.showAsDropDown構(gòu)造方法
一個參數(shù)
?三個參數(shù),其中xoff,yoff表示向x,y軸偏移
實例如下:
popupWindow.showAsDropDown(view,100,100);
?也可以使用如下方法:
popupWindow.showAsDropDown(view,view.getWidth(),-view.getHeight());
?設(shè)置偏移效果如下:
?3.1實現(xiàn)點擊空白處,退出popupwindow
使用上述:文章來源:http://www.zghlxwxcb.cn/news/detail-773675.html
4.?setFocusable?(?boolean?focusable?) | 設(shè)置是否獲取焦點 |
文章來源地址http://www.zghlxwxcb.cn/news/detail-773675.html
到了這里,關(guān)于安卓開發(fā)——控件AlertDialog實現(xiàn)方式,設(shè)置下部三個按鈕,自定義布局設(shè)置.setView(dialogView)樣式,控件PopupWindow1常用方法,showAsDropDown構(gòu)造方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!