Andorid——自定义Dialog

目录

创建自定义Dialog步骤

Shape用法

Style样式资源

创建自定义Dialog步骤

自定义Dialog就是自己对弹出Dialog框的内容、格式进行设置,而不用系统定义好的样式。

1.首先创建个一个弹出框的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">
    <TextView
        android:id="@+id/textview_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标题"
        android:gravity="center"
        android:padding="10dp"
         style="@style/MytextView"/>
    <TextView
        android:id="@+id/textview_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="内容"
        android:padding="10dp"
        android:gravity="center"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="取消"
        android:textSize="15dp"
        android:textColor="@color/white"
        android:background="@drawable/button_background_select"/>
    <Button
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/button_right_background_select"
        android:text="确定"
        android:textSize="15dp"
        android:textColor="@color/white"/>

    </LinearLayout>

2.在Mainactivity中进行设置,代码如下

  private void mydialog() {
        dialog=new Dialog(MainActivity.this, R.style.NoDialogTitle);//初始化Dialog,并设置Dialog的风格,这里引用了style文件中的NoDialogTitle
        mInflater=getLayoutInflater();
        final View dialogView=mInflater.inflate(R.layout.my_dialog,null);
        TextView textView_title= (TextView) dialogView.findViewById(R.id.textview_title);
        TextView textView_message= (TextView) dialogView.findViewById(R.id.textview_message);
        Button btn_cancel= (Button) dialogView.findViewById(R.id.btn_cancel);
        Button btn_ok= (Button) dialogView.findViewById(R.id.btn_ok);
        textView_title.setText("新标题");   
        textView_message.setText("新内容");
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "点击确定", Toast.LENGTH_SHORT).show();
                dialog.dismiss();  //关闭dialog界面
            }
        });
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉Dialog默认的标题
        dialog.setContentView(dialogView);//将dialogView设置以为一个显示视图
        dialog.show();
    }

这里写图片描述

是不是有点巨丑无比,没办法,咱也不是专业做UI的,嘿嘿。

Shape用法

如果你观察仔细,相信你已经注意到了上面取消跟确定按键的左下角的直角变成了一个圆角,当然,这里不是用背景图片设置的而是通过对矩形的塑形。
1.在drawable中建立一个xml文件,进行如下设置

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:bottomLeftRadius="@8dp"></corners>//设置矩形左下角的弧度
    <solid android:color="@color/green"></solid> //内部填充色
  <stroke android:width="1dp"  //添加一个边框线,宽度为1dp
      android:color="@color/white"></stroke>//将边框线设置为白色

</shape>

2.在自定义的dialog的layout文件中要进行塑形的按钮进行如下添加即可

android:background="@drawable/background"

Style样式资源

样式资源

样式资源定义了用户界面(UI)的格式和外观。样式能被应用到单独的View (通过置入layout 文件),或者整个Activity及应用程序(通过置入manifest文件)。

关于创建及应用样式的更多信息,请参阅应用样式和主题。

注意:样式是简单类型资源,是用名称(name)属性(而非XML文件名)来直接引用的。因此,在一个XML文件里,可以把样式资源和其他简单类型资源一起放入一个元素下。

文件位置

res/values/filename.xml

文件名可随意指定。元素的名称name将被用作资源ID。

资源引用

XML代码: @[package:]style/style_name

eg:
在res/value/styles中进行如下代码设置

 </style>
    <style name="MytextView">
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">15sp</item>
        <item name="android:background">#995544</item>
    </style>

然后在res/layout/my_dialog中的添加代码:

    <TextView
        android:id="@+id/textview_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标题"
        android:gravity="center"
        android:padding="10dp"
         style="@style/MytextView"/>//这里添加样式

这样就将TextView中添加了style中设置的属性了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值