手把手教你魯一個屬于自己風格的dialog

最近項目里面有個需求,需要自定義一個dialog。廢話不多說,直接上代碼:

轉(zhuǎn)載注明出處:http://www.itdecent.cn/p/d520312e0fe4

個人github鏈接:?https://github.com/? ? ? ?喜歡的小伙伴可以 start 一下

項目開始之前,請確認已經(jīng)創(chuàng)建了一個完整可以跑的項目工程(如果沒有可以自行度娘哦?。?,完成之后我們的列車就要出發(fā)了。






1. 自定義控件,行業(yè)規(guī)矩繼承一個父類控件?

? public classCustomMessageConfirmDialogextendsDialogimplementsView.OnClickListener{


}


2. 重寫父類構(gòu)造方法


public CustomMessageConfirmDialog(Context context){

? ? ? ?this(context,R.style.dialog_costum_stytle);

}

public CustomMessageConfirmDialog(Context context,String title,String content){

? ? ? ? ?this(context,R.style.dialog_costum_stytle);

}

public CustomMessageConfirmDialog(Context context,int themeResId){

? ? ? ? super(context,themeResId);

? ? ? ? mContext=context;

? ? ? ? this.title=title;

? ? ? ? this.content=content;

}


3.? 這里我們傳入了一個我們自定義的? style 樣式文件,規(guī)定一下dialog的樣式風格和入場出場動畫(當然網(wǎng)上好多人喜歡用代碼的形式來處理,這里就不深究了,效果是一樣的)?




4. 這里面涉及了兩個dialog的動畫。一個是入場動畫,一個是出場的動畫。


Android原生的dialog動畫樣式。

在參考了Android原生的dialog后,決定對代碼進行如下的修改:


4.1? 我們同樣的在style文件中創(chuàng)建標簽為? DialogAnimation? 的樣式文件,繼承父類? parent="android:Animation"

4.2 在標簽的內(nèi)部定義兩個子標簽,分別是?

需要注意的是這里的? @android:windowEnterAnimation 就是重寫了父類的windowEnterAnimation和windowExitAnimation這兩個屬性


4.3 入場動畫引用? @anim/dialog_in? 需要在? res —> anim? ? 中定義自己的入場動畫? ,文件名為dialog_in.xml ,代碼如下:





4.5? 同樣的出場動畫引用了? @anim/dialog_exit? 需要在? res —> anim? ? 中定義自己的入場動畫? ,文件名為dialog_exit.xml ,代碼如下:






到這里動畫部分就算完成了。

那我們來看一下,


5. dialog的內(nèi)容部分 ? ?

@Override

protected voidonCreate(BundlesavedInstanceState){

? ? ? ? ? ?super.onCreate(savedInstanceState);

? ? ? ? ? ?setContentView(R.layout.dialog_message_confirm_custom);

? ? ? ? ? ?//初始化view的操作

? ? ? ? ? ?initView();



? ? ? ? ? //不能通過window設置去除標題欄和邊框操作,根據(jù)源碼提示需要通過styles.xml設置

? ? ? ? ? ?initListener();

? ? ? ? ? ?initData();

}



6. ?布局文件

?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_round_white"

android:orientation="vertical">



<TextView

android:id="@+id/title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:padding="12dp"

android:layout_marginTop="12dp"

android:text="提示"

android:textSize="18sp"

android:textColor="@color/font_common_dialog_title"/>


<TextView

android:id="@+id/content"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_gravity="center_horizontal"

android:lineSpacingExtra="3dp"

android:layout_marginLeft="40dp"

android:layout_marginTop="20dp"

android:layout_marginRight="40dp"

android:layout_marginBottom="30dp"

android:text="簽到成功,獲得200積分"

android:textSize="14sp"

android:textColor="@color/font_common_dialog_context"/>


<TextView

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/commom_background"/>

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">


<TextView

android:id="@+id/cancel"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_dialog_left_white"

android:layout_weight="1.0"

android:gravity="center"

android:text="取消"

android:textSize="14sp"

android:textColor="@color/font_color_cancal"/>


<TextView

android:layout_width="1dp"

android:layout_height="match_parent"

android:background="@color/commom_background"/>

android:id="@+id/submit"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_dialog_right_white"

android:gravity="center"

android:layout_weight="1.0"

android:text="確定"

android:textSize="14sp"

android:textColor="@color/font_color_confirm"/>

</LinearLayout>

</LinearLayout>



7. shrpe文件? 繪制邊框圓角? (在 drawable中創(chuàng)建? bg_dialog_left_white.xml的文件

<shapexmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

<solidandroid:color="@color/white"/>

<cornersandroid:bottomLeftRadius="8dp"/>

</shape>

8.? 在textView中引用? android:background="@drawable/bg_dialog_left_white"


色彩值在 values -> colors.xml文件中定義 ?




到這里完成了








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

相關閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,334評論 0 17
  • 本人初學Android,最近做了一個實現(xiàn)安卓簡單音樂播放功能的播放器,收獲不少,于是便記錄下來自己的思路與知識總結(jié)...
    落日柳風閱讀 19,454評論 2 41
  • 一、Welcome延時跳轉(zhuǎn)界面(1s跳轉(zhuǎn)) 二、登陸界面(調(diào)用數(shù)據(jù)庫) 輸入用戶名、密碼,點擊注冊,出現(xiàn)Toast...
    Android課程筆記閱讀 2,070評論 0 5
  • 今晚流氓兔推薦的歌曲是《白晝之夜》,純音樂。聽著好舒緩的一首輕音樂,整個人都會放松好多。 突然想問一句:你過得幸福...
    佐恬閱讀 271評論 0 0
  • 技術只是工具 金錢也一樣, 也只是一種工具 太過重視或太過忽視工具,都是不好的,從哲理上講,凡事都有一個度,把握好...
    kamfon閱讀 155評論 0 0

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