如何与在右上角关闭按钮在Android的活动?按钮、Android

2023-09-12 04:19:25 作者:过期的情话

我想有一个活动的其中将包括在右上角关闭活动关闭按钮,即X。任何对此的例子将是非常有益的。

I would like to have an activity which would consist of a close button i.e 'x' at the right top corner closing the activity. Any examples on this will be very much helpful.

推荐答案

作出活动透明主题清单如下:

make an Activity with Transparent theme in manifest like this:

<activity android:name=".MyDialogActivity" android:theme="@style/Theme.Transparent" />

再这样定义布局:

and then define layout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_margin="15dp" android:padding="2dp">
        <!-- Contents will go here..-->
    </RelativeLayout>
    <Button android:layout_alignParentRight="true" android:text="X"
        android:textColor="#FFF" android:background="@drawable/round_button_background"
        android:gravity="center_vertical|center_horizontal"
        android:layout_margin="5dp" android:layout_height="25dp"
        android:layout_width="25dp" android:textSize="12sp" android:textStyle="bold"
        android:onClick="cancelActivity" />
</RelativeLayout>

和定义背景为你的亲密在绘制这样的一个按钮

and define a background for your close Button in drawable like this:

round_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    <solid android:color="#9F2200" />
    <stroke android:width="2dp" android:color="#FFF" />
</shape>