AlertDialog中shape的使用方法

这里写图片描述

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>

运行图

这里写图片描述

双彩虹

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Service使用AlertDialog需要注意一些问题,因为Service并没有UI界面,所以需要在Service创建一个Handler来处理AlertDialog的显示。 以下是在Service使用AlertDialog的步骤: 1. 在Service创建一个Handler,用于处理AlertDialog的显示。 ```java private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW_DIALOG: showDialog(); break; case DISMISS_DIALOG: dismissDialog(); break; default: break; } } }; ``` 2. 在Service创建一个AlertDialog,并在需要显示AlertDialog时发送一个 Message 给 Handler。 ```java private void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage("这是一个AlertDialog"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定按钮后的操作 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消按钮后的操作 } }); mAlertDialog = builder.create(); mAlertDialog.show(); } private void dismissDialog() { if (mAlertDialog != null) { mAlertDialog.dismiss(); } } ``` 3. 在Service接收来自其他组件的指令,并根据指令发送不同的 Message 给 Handler。 ```java @Override public int onStartCommand(Intent intent, int flags, int startId) { int command = intent.getIntExtra("command", 0); switch (command) { case SHOW_DIALOG: mHandler.sendEmptyMessage(SHOW_DIALOG); break; case DISMISS_DIALOG: mHandler.sendEmptyMessage(DISMISS_DIALOG); break; default: break; } return super.onStartCommand(intent, flags, startId); } ``` 通过以上步骤,在Service就可以使用AlertDialog了。需要注意的是,在Service使用AlertDialog时,需要在AndroidManifest.xml声明权限 android.permission.SYSTEM_ALERT_WINDOW。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值