Android實現(xiàn)自定義dialog的代碼
更新時間:2018年11月11日 13:12:19 作者:給你留燈
這篇文章主要介紹了Android實現(xiàn)自定義dialog的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
Android自定Dialog
先上效果圖:

1.先在drawable下新建一個drawble resource file,這個文件用于dialog的圓角背景
<?xml version="1.0" encoding="utf-8"?> 2.在layout下新建一個xml文件,這個布局的背景使用剛剛定義的drawable文件,android:background="@drawable/建的drawable文件" <?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="操作遙控器"
android:textColor="#585858"
android:textSize="25dp"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#585858"
android:text="按開關/模式/溫度加減任意一鍵學習"
android:textSize="20dp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal"
android:padding="10dp"
>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@mipmap/yaokong"
android:layout_marginLeft="35dp"
/>
<!--<ImageView-->
<!--android:layout_width="100dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:src="@mipmap/yaokong"-->
<!--android:layout_marginLeft="35dp"-->
<!--/>-->
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginBottom="5dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:src="@mipmap/xuanhuang" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="等待學習按鍵..."
android:textColor="#585858"
android:textSize="20dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/yaokongCancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="1dp"
android:background="#8d8d8f"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="8dp"
android:textColor="#1196db"
android:textSize="25dp"
android:text="取消"
/>
</LinearLayout>
3.在values的styles設置dialog樣式
4.之后去顯示
package com.example.atry.test;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
public class tianjiayaokong extends AppCompatActivity {
// 這個為點擊顯示dialog的布局
private LinearLayout kongtiaol;
// dialog中的取消
private LinearLayout yaokongCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tianjiayaokong);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
actionBar.hide();
}
kongtiaol = findViewById(R.id.kongtiaol);
kongtiaol.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
}
/**
* 顯示dialog
*/
private void showDialog()
{
LayoutInflater inflater = getLayoutInflater();
//通過inflate加載出自定義布局
View view = inflater.inflate(R.layout.activity_dialog_componet,null);
final Dialog dialog = new Dialog(this,R.style.custom_dialog);
dialog.setContentView(view);
yaokongCancel = view.findViewById(R.id.yaokongCancel);
yaokongCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
}
總結
以上所述是小編給大家介紹的Android實現(xiàn)自定義dialog的代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
您可能感興趣的文章:
- Android 自定義AlertDialog對話框樣式
- Android自定義對話框Dialog的簡單實現(xiàn)
- Android中DialogFragment自定義背景與寬高的方法
- android自定義ProgressDialog加載效果
- Android自定義彈出框dialog效果
- Android通過自定義Activity實現(xiàn)懸浮的Dialog詳解
- Android仿IOS自定義AlertDialog提示框
- Android實現(xiàn)自定義圓角對話框Dialog的示例代碼
- android自定義AlertDialog對話框
- ANDROID中自定義對話框AlertDialog使用示例
- Android AlertDialog自定義樣式實現(xiàn)代碼
- Android自定義Dialog實現(xiàn)文字動態(tài)加載效果
相關文章
Android系統(tǒng)進程間通信(IPC)機制Binder中的Server啟動過程源代碼分析
本文主要介紹Android IPC機制Binder中的Server啟動過程源代碼,這里對Binder 中Server 啟動過程中的源碼做了詳細的介紹,有研究Android源碼 Binder 通信的小伙伴可以參考下2016-08-08
Android實現(xiàn)圖片自動輪播并且支持手勢左右無限滑動
這篇文章給大家介紹android實現(xiàn)圖片自動輪播并且支持手勢左右無限滑動,代碼簡單易懂,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-10-10
Android獲取手機屏幕寬高、狀態(tài)欄高度以及字符串寬高信息的方法
這篇文章主要介紹了Android獲取手機屏幕寬高、狀態(tài)欄高度以及字符串寬高信息的方法,涉及Android獲取文字寬高、狀態(tài)欄高度、textView寬度及屏幕尺寸的相關技巧,需要的朋友可以參考下2015-04-04
Android HorizontalScrollView內子控件橫向拖拽實例代碼
本文主要介紹Android HorizontalScrollView的使用,這里給大家一個實例來展示HorizontalScrollView內子控件橫向拖拽的效果實現(xiàn),有需要的小伙伴可以參考下2016-07-07
Android 組件Gallery和GridView示例講解
本文主要講解Android 組件Gallery和GridView,這里詳細介紹組件Gallery和GridView的知識要點,并附示例代碼和實現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-08-08

