这篇文章主要介绍了Android如何实现自定义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如何实现自定义dialog”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。