仿IOS彈窗/精美彈窗

先上效果圖


//自定義style,讓彈窗引用


布局:寫彈窗樣式

效果圖:




布局代碼:

畫個(gè)Shape,下面布局代碼會(huì)用到,給個(gè)圓角顏色:



<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shape_ui_alert_view" android:orientation="vertical" >

?<TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="標(biāo)題" android:textColor="@color/text_black" android:textSize="16sp" android:textStyle="bold" />

?<TextView android:id="@+id/tvMessage" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="內(nèi)容" android:textSize="16sp" />

?<View android:layout_width="match_parent"

?android:layout_height="0.5dp"

?android:background="@color/line_gray" />

?<LinearLayout

android:layout_width="match_parent"

?android:layout_height="40dp"

?android:orientation="horizontal" >

?<TextView

?android:id="@+id/tvBtnLeft"

android:layout_width="match_parent"

?android:layout_height="match_parent"

?android:layout_weight="1"

?android:gravity="center" android:text="取消" android:textSize="14sp" />

?<View

?android:layout_width="0.3dp"

?android:layout_height="match_parent"

?android:background="#E0E0E0" />

<TextView

android:id="@+id/tvBtnRight"

android:layout_width="match_parent"

?android:layout_height="match_parent"

?android:layout_weight="1"

android:gravity="center" android:text="確定" android:textSize="14sp" />

?</LinearLayout>



//自定義類繼承Dialog

public class UIAlertViewextends Dialog {

private Contextcontext;

private Stringtitle;

private Stringmessage;

private StringbuttonLeftText;

private StringbuttonRightText;

private ClickListenerInterfaceclickListenerInterface;

public UIAlertView(Context context, String title, String message,

String buttonLeftText, String buttonRightText) {


super(context, R.style.UIAlertViewStyle);

this.context = context;

this.title = title;

this.message = message;

this.buttonLeftText = buttonLeftText;

this.buttonRightText = buttonRightText;

}

public interface ClickListenerInterface {

public void doLeft();

public void doRight();

}

@Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

inite();

}

public void inite() {

LayoutInflater inflater = LayoutInflater.from(context);


//這里是加載布局,上面已經(jīng)也好了

View view = inflater.inflate(R.layout.ui_alert_view,null);

setContentView(view);


//獲取布局中的ID

TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);

TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);

TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);

TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);

if ("".equals(title)) {

tvTitle.setVisibility(View.GONE);

}else {

tvTitle.setText(title);

}

tvMessage.setText(message);

tvLeft.setText(buttonLeftText);

tvRight.setText(buttonRightText);

tvLeft.setOnClickListener(new clickListener());

tvRight.setOnClickListener(new clickListener());

Window dialogWindow = getWindow();

WindowManager.LayoutParams lp = dialogWindow.getAttributes();

DisplayMetrics d =context.getResources().getDisplayMetrics();

lp.width = (int) (d.widthPixels *0.8);

dialogWindow.setAttributes(lp);

}

public void setClicklistener(ClickListenerInterface clickListenerInterface) {

this.clickListenerInterface = clickListenerInterface;

}

private class clickListenerimplements View.OnClickListener {

@Override

? ? ? ? public void onClick(View v) {

int id = v.getId();

switch (id) {

//左邊的點(diǎn)擊事件

case R.id.tvBtnLeft:

clickListenerInterface.doLeft();

break;



//右邊的點(diǎn)擊事件

case R.id.tvBtnRight:

clickListenerInterface.doRight();

break;

default:

break;

}

}

}

;

}




這里是具體使用

final UIAlertView delDialog =new UIAlertView(mContext,"溫馨提示","確認(rèn)刪除該商品嗎?",

"取消","確定");

delDialog.show();

delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {

@Override

public void doLeft() {

//左邊按鈕的點(diǎn)擊事件 //.dismiss()消失彈窗

delDialog.dismiss();

}

//有點(diǎn)按鈕的點(diǎn)擊事件

@Override

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void doRight() {

delDialog.dismiss();

}

}

);


結(jié)束!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容