Android編程自定義扁平化對話框示例
本文實例講述了Android編程自定義扁平化對話框。分享給大家供大家參考,具體如下:
平時我們開發(fā)的大多數(shù)的Android、iOS的APP,它們的風格都是擬物化設(shè)計。如Android 4.X、iOS 7、WP8采用的是扁平化設(shè)計,可以看出扁平化設(shè)計是未來UI設(shè)計的趨勢。其實扁平化設(shè)計要比擬物化設(shè)計要簡單一點,扁平化設(shè)計更加的簡約,給人視覺上更加舒服。
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" >
<!-- 標題欄 -->
<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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android自定義EditText實現(xiàn)登錄界面
- Android取消EditText自動獲取焦點默認行為
- Android控件系列之EditText使用方法
- android同時控制EditText輸入字符個數(shù)和禁止特殊字符輸入的方法
- Android中EditText實現(xiàn)不可編輯解決辦法
- Android定制自己的EditText輕松改變底線顏色
- Android中EditText顯示明文與密碼的兩種方式
- Android更改EditText下劃線顏色樣式的方法
- android基礎(chǔ)教程之a(chǎn)ndroid的listview與edittext沖突解決方法
- Android EditText實現(xiàn)扁平化的登錄界面
相關(guān)文章
Android中AnimationDrawable使用的簡單實例
這篇文章介紹了Android中AnimationDrawable使用的簡單實例,有需要的朋友可以參考一下2013-10-10
android實現(xiàn)簡易登錄注冊界面及邏輯設(shè)計
這篇文章主要為大家詳細介紹了android實現(xiàn)簡易登錄注冊界面及邏輯設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Android使用DrawerLayout實現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細介紹了Android使用DrawerLayout實現(xiàn)側(cè)滑菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法
這篇文章主要介紹了Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法,其中View轉(zhuǎn)換時作者特別提到了getDrawingCache=null問題的解決方法,需要的朋友可以參考下2016-03-03
android使用surfaceview+MediaPlayer播放視頻
這篇文章主要為大家詳細介紹了android使用surfaceview+MediaPlayer播放視頻,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Android 自定義view實現(xiàn)水波紋動畫效果
這篇文章主要介紹了 Android 自定義view實現(xiàn)水波紋動畫效果的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01
Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用
Retrofit和OkHttp API以及JVM擴展RxJava都是開源項目,大家可以輕松在GitHub上找到,下載和基本配置部分這里我們不作重點,主要還是來看一下Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用:2016-06-06
Android 中ScrollView與ListView沖突問題的解決辦法
這篇文章主要介紹了Android 中ScrollView與ListView沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握解決問題的辦法,需要的朋友可以參考下2017-10-10
Android實現(xiàn)ListView異步加載的方法(改進版)
這篇文章主要介紹了Android實現(xiàn)ListView異步加載的方法,針對前面介紹的方法進行了線程操作的改進,具有一定參考借鑒價值,需要的朋友可以參考下2016-08-08

