Android 自定义AlertDialog,调用方法与系统一致
2019獨角獸企業重金招聘Python工程師標準>>>
由于android原生的AlertDialog都一致,有時為了和你的項目的Dialog保持一致,你最先想到的就是有沒有AlertDialog相關的style,但據我的查找,官方沒有提供明確的文檔來修改其樣式,所以我們想到的是自己自定一個AlertDialog
如圖,當然風格可以自己修改
所先把你想要的布局custom_dialog_view
<?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:orientation="vertical"
? ? android:gravity="center" >
? ? <!-- 頂部橢園邊緣 -->
? ? ?<ImageView
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="22dp"
? ? ? ? android:src="@drawable/app_listbk_d" >
? ? </ImageView>
? ? <!-- 中間白色背景,兩個TextView,標題和內容,留一個LinearLayout,在代碼中根據調用動態加上按鈕 -->
? ? <LinearLayout
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="@drawable/app_listbk_d"
? ? ? ? android:orientation="vertical" >
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/title"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:textColor="#000000"
? ? ? ? ? ? android:textSize="35dp" />
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/message"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_marginBottom="10dp"
? ? ? ? ? ? android:layout_marginLeft="20dp"
? ? ? ? ? ? android:layout_marginRight="10dp"
? ? ? ? ? ? android:layout_marginTop="20dp"
? ? ? ? ? ? android:textColor="#000000"
? ? ? ? ? ? android:textSize="25dp" />
? ? ? ? <!-- 在LinearLayout中加按鈕 -->
? ? ? ? <LinearLayout
? ? ? ? ? ? android:id="@+id/buttonLayout"
? ? ? ? ? ? android:layout_width="fill_parent"
? ? ? ? ? ? android:layout_height="fill_parent"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:orientation="horizontal" >
? ? ? ? </LinearLayout>
? ? </LinearLayout>
? ? <!-- 底部橢園邊緣 -->
? ? ?<ImageView
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="22dp"
? ? ? ? android:layout_marginTop="-2dp"
? ? ? ? android:src="@drawable/app_listbk_d" >
? ? </ImageView>
</LinearLayout>
然后借助原生的AlertDialog來重寫一下這個控制來達到我們的需求
package com.example.testapi.widget;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.example.testapi.R;
public class AlertDialog {
? ? Context context;
? ? android.app.AlertDialog ad;
? ? TextView titleView;
? ? TextView messageView;
? ? LinearLayout buttonLayout;
? ? public AlertDialog(Context context) {
? ? ? ? this.context = context;
? ? ? ? ad = new android.app.AlertDialog.Builder(context).create();
? ? ? ? ad.show();
? ? ? ? // Replace the source alert dialog.
? ? ? ? Window window = ad.getWindow();
? ? ? ? window.setContentView(R.layout.custom_dialog_view);
? ? ? ? titleView = (TextView) window.findViewById(R.id.title);
? ? ? ? messageView = (TextView) window.findViewById(R.id.message);
? ? ? ? buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
? ? }
? ? public void setTitle(int resId)
? ? {
? ? ? ? titleView.setText(resId);
? ? }
? ? public void setTitle(String title) {
? ? ? ? titleView.setText(title);
? ? }
? ? public void setMessage(int resId) {
? ? ? ? messageView.setText(resId);
? ? }
? ? public void setMessage(String message)
? ? {
? ? ? ? messageView.setText(message);
? ? }
? ? /**
? ? ?* Button style
? ? ?*?
? ? ?* @param text
? ? ?* @param listener
? ? ?*/
? ? public void setPositiveButton(String text, final View.OnClickListener listener)
? ? {
? ? ? ? Button button = new Button(context);
? ? ? ? LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
? ? ? ? ? ? ? ? LayoutParams.WRAP_CONTENT);
? ? ? ? button.setLayoutParams(params);
? ? ? ? button.setBackgroundResource(R.drawable.app_listbk);
? ? ? ? button.setText(text);
? ? ? ? button.setTextColor(Color.WHITE);
? ? ? ? button.setTextSize(20);
? ? ? ? button.setOnClickListener(listener);
? ? ? ? buttonLayout.addView(button);
? ? }
? ? /**
? ? ?* ?Button style
? ? ?*?
? ? ?* @param text
? ? ?* @param listener
? ? ?*/
? ? public void setNegativeButton(String text, final View.OnClickListener listener)
? ? {
? ? ? ? Button button = new Button(context);
? ? ? ? LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
? ? ? ? ? ? ? ? LayoutParams.WRAP_CONTENT);
? ? ? ? button.setLayoutParams(params);
? ? ? ? button.setBackgroundResource(R.drawable.app_listbk);
? ? ? ? button.setText(text);
? ? ? ? button.setTextColor(Color.WHITE);
? ? ? ? button.setTextSize(20);
? ? ? ? button.setOnClickListener(listener);
? ? ? ? if (buttonLayout.getChildCount() > 0)
? ? ? ? {
? ? ? ? ? ? params.setMargins(20, 0, 0, 0);
? ? ? ? ? ? button.setLayoutParams(params);
? ? ? ? ? ? buttonLayout.addView(button, 1);
? ? ? ? } else {
? ? ? ? ? ? button.setLayoutParams(params);
? ? ? ? ? ? buttonLayout.addView(button);
? ? ? ? }
? ? }
? ? /**
? ? ?* dismiss dialog
? ? ?*/
? ? public void dismiss() {
? ? ? ? ad.dismiss();
? ? }
}
這樣我們在自己的項目中就可以像使用原生的AlertDialog一樣正常使用了
final AlertDialog ad = new AlertDialog(DialogStyleActivity.this);
? ? ad.setTitle("標題");
? ? ad.setMessage("dkdkkdkdk順水有晨在于 在在在在在礙御用有有有地區專業分工有地介是的和無本之木工;地有膽有關有關 " +
? ? "耨溪水源源源碼在緊俏商品22是否 2進行大躍進要核工業部");
? ? ad.setPositiveButton("確定", new OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ad.dismiss();
? ? ? ? ? ? Toast.makeText(DialogStyleActivity.this, "被點到確定", Toast.LENGTH_LONG).show();
? ? ? ? }
? ? });
? ? ad.setNegativeButton("取消", new OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ad.dismiss();
? ? ? ? ? ? Toast.makeText(DialogStyleActivity.this, "被點到取消", Toast.LENGTH_LONG).show();
? ? ? ? }
? ? });
? ? }
轉載于:https://my.oschina.net/u/1244156/blog/205750
總結
以上是生活随笔為你收集整理的Android 自定义AlertDialog,调用方法与系统一致的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Wowza® Media Systems
- 下一篇: mysql 存储过程乱码的问题