AlertDialog中shape的使用方法

本文详细介绍了如何在Android应用中自定义对话框及其内部按钮的样式,包括使用XML布局文件创建自定义形状、调整按钮状态时的外观变化,并通过styles.xml文件实现样式的一致性和复用性。

这里写图片描述

shape是用来设置背景图、按钮等样式的一种方法

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 private Button mBtn5;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  mBtn5 = (Button) findViewById(R.id.button_dialog5);
   mBtn5.setOnClickListener(this);
   }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
         case R.id.button_dialog5:
                showMydialog();
                break;
           default:
                break;      
                }
          }
 private void showMydialog() {
        dialog = new Dialog(MainActivity.this, R.style.NoDialogTitle);//styleDialogTitle写在res/values/styles下面,是为了去掉原有的背景文本框样式,便于加上自己设计的样式。
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.my_dialog, null);
        TextView textViewTitle = (TextView) dialogView.findViewById(R.id.textview_title);
        TextView textViewMessage = (TextView) dialogView.findViewById(R.id.textview_message);
        Button button_cancel = (Button) dialogView.findViewById(R.id.button_cancel);
        Button button_ok = (Button) dialogView.findViewById(R.id.button_ok);
        textViewTitle.setText("这是新设置的标题");
        textViewMessage.setText("这是新设置的内容");
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        button_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "点击的是确定", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        dialog.setContentView(dialogView);
        dialog.show();
    }
    }

my_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/dialog_background">
    <TextView
        android:id="@+id/textview_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是一个标题"
        android:background="@drawable/dialog_title_background"
        style="@style/myTextView"//style写在res/values/styles下
        />
    <TextView
        android:id="@+id/textview_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是内容"
        android:gravity="center"
        android:textSize="20sp"
        android:padding="10dp"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/button_cancel"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/btn_right_background"
        android:text="取消"/>
    <Button
        android:id="@+id/button_ok"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/btn_left_background"
        android:text="确定"/>
</LinearLayout>
</LinearLayout>

btn_left_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_left_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_left_normal"/>
</selector>

btn_left_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
// <dimen name="corners">10dp</dimen>把半径长度全部写在res/values/dimens下,方便以后改变长度。
// <corners android:bottomRightRadius="10dp"/>
    <corners android:bottomRightRadius="@dimen/corners"/>
    <solid android:color="#79DFF6"/>
    <stroke android:color="@android:color/white" android:width="5dp"/>
</shape>

btn_left_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomRightRadius="@dimen/corners"/>
    <solid android:color="#30AEE8"/>
    <stroke android:color="@android:color/white" android:width="5dp"/>
</shape>

btn_right_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_right_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_right_normal"/>
</selector>

btn_right_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomLeftRadius="@dimen/corners"/>
    <solid android:color="#79DFF6"/>
    <stroke android:color="@android:color/white" android:width="5dp"/>
</shape>

btn_right_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomLeftRadius="@dimen/corners"/>
    <solid android:color="#30AEE8"/>
    <stroke android:color="@android:color/white" android:width="5dp"/>
</shape>

dialog_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="15dp"/>
    <solid android:color="#8dff3d"/>
    <stroke android:color="@android:color/white" android:width="5dp"/>
</shape>

dialog_title_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="@dimen/corners"/>
    <solid android:color="#8dff3d"/>
    <stroke android:color="@android:color/white" android:width="1dp"/>
</shape>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
    <style parent="@android:Theme.Dialog" name="NoDialogTitle">
      //窗口框架为空,即框架为透明的。
        <item name="android:windowFrame">@null</item>
      //去掉标题
        <item name="android:windowNoTitle">true</item>
      //将窗口背景设置为透明的
        <item name="android:windowBackground">@android:color/transparent</item>
      //窗体设置为悬浮的
        <item name="android:windowIsFloating">true</item>
     //窗体内容不覆盖
        <item name="android:windowContentOverlay">@null</item>


    </style>
    //TextView的文本颜色,字体大小,填充宽度,方便TextView直接调用。
<style name="myTextView">
    <item name="android:textColor">#ff0000</item>
    <item name="android:textSize">25sp</item>
    <item name="android:padding">10dp</item>
</style>
</resources>

运行图

这里写图片描述

双彩虹

这里写图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值