怎么在Android中自定義一個扁平化對話框

這篇文章給大家介紹怎么在Android中自定義一個扁平化對話框,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)曲水,十年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

Shamoo想到在Android平臺上弄一個扁平化的對話框。參考過一篇帖子,然后改了一下。

這個Demo比較簡單,首先是一個dialog的布局文件,這個dialog的布局要實例化成對話框可以通過AlertDialog.Builder的setView方法,將LayoutInflater實例化的dialog布局設(shè)置對話框具體顯示內(nèi)容。

DialogWindows.Java

package com.example.dialogwindows;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class DialogWindows extends Activity {
  private Button button;
  private View dialogView;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button) findViewById(R.id.btn);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Builder builder = myBuilder(DialogWindows.this);
        final AlertDialog dialog = builder.show();
        //點擊屏幕外側(cè),dialog不消失
        dialog.setCanceledOnTouchOutside(false);
        Button btnOK = (Button) dialogView.findViewById(R.id.btn_ok);
        btnOK.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            Toast.makeText(DialogWindows.this, "你點擊了確定按鈕", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
          }
        });
        Button btnCancel = (Button) dialogView.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          Toast.makeText(DialogWindows.this, "你點擊了取消按鈕", Toast.LENGTH_SHORT).show();
          dialog.dismiss();
        }
      });
        ImageButton customviewtvimgCancel = (ImageButton) dialogView.findViewById(R.id.btn_exit);
        customviewtvimgCancel.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            Toast.makeText(DialogWindows.this, "你點擊了退出按鈕", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
          }
        });
      }
    });
  }
  protected Builder myBuilder(Context context) {
    LayoutInflater inflater = getLayoutInflater();
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    dialogView = inflater.inflate(R.layout.dialog, null);
    return builder.setView(dialogView);
  }
}

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <!-- 標(biāo)題欄 -->
  <RelativeLayout
    android:id="@+id/dialog_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#1A94F9" >
    <TextView
      android:id="@+id/tv_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:padding="10dp"
      android:text="@string/about"
      android:textColor="#000000" />
    <ImageButton
      android:id="@+id/btn_exit"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:layout_alignParentRight="true"
      android:layout_centerVertical="true"
      android:background="@drawable/canceltor" />
  </RelativeLayout>
  <!-- 顯示的內(nèi)容 -->
  <LinearLayout
    android:id="@+id/dialog_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_below="@id/dialog_title"
    android:padding="20dp" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/author"
      android:textColor="#ffffff" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:linksClickable="true"
      android:text="@string/blog"
      android:textColor="#ffffff" />
  </LinearLayout>
  <!-- 底部按鈕 -->
  <LinearLayout
    android:id="@+id/customviewlayLink"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/dialog_msg"
    android:orientation="horizontal"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp" >
    <Button
      android:id="@+id/btn_ok"
      android:layout_width="fill_parent"
      android:layout_height="40dp"
      android:background="@drawable/linkbtnbged"
      android:linksClickable="true"
      android:layout_weight="1"
      android:layout_marginRight="10dp"
      android:text="@string/btn_ok" />
    <Button
      android:id="@+id/btn_cancel"
      android:layout_width="fill_parent"
      android:layout_height="40dp"
      android:linksClickable="true"
      android:background="@drawable/linkbtnbged"
      android:text="@string/btn_cancel"
      android:layout_marginLeft="10dp"
      android:layout_weight="1" />
  </LinearLayout>
</RelativeLayout>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/show_dialog" />
</RelativeLayout>

關(guān)于怎么在Android中自定義一個扁平化對話框就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當(dāng)前題目:怎么在Android中自定義一個扁平化對話框
文章鏈接:http://bm7419.com/article48/iihihp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化外貿(mào)網(wǎng)站建設(shè)、企業(yè)建站網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)