【发布时间】:2017-08-03 04:02:09
【问题描述】:
我想要一个包含关闭按钮的活动,即右上角的“x”关闭活动。任何这方面的例子都会非常有帮助。
【问题讨论】:
我想要一个包含关闭按钮的活动,即右上角的“x”关闭活动。任何这方面的例子都会非常有帮助。
【问题讨论】:
在清单中创建一个带有透明主题的Activity,如下所示:
<activity android:name=".MyDialogActivity" android:theme="@style/Theme.Transparent" />
然后像这样定义布局:
<?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>
并为您关闭的Button 定义背景,如下所示:
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>
【讨论】:
Activity 设置为透明并将RelativeLayout 背景设置为您想要的任何颜色时,您会看到它位于屏幕的中心
使用 juzt 在右上角有一个类似 x 的图像。通过按 x 图像你可以完成活动。
XML:
<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:align_parentRight="true"
android:src="@drawable/closeimage"
/>
在 Java 代码中:
ImageView view=(ImageView)findViewById(R.id.close);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(currentclass.this,statingclass.class);
startActivity(intent);// here u can start another activity or just call finish method to close the activity.
finish();
}
});
【讨论】:
在你的xml中添加一个按钮视图
语法:
<Button android:id="+@id/close"
android:text="close"
android:layout_margin="5sp"
android:layout_height="25sp"
android:layout_width="25sp"
android:onClick="closeActivity" />
在你的活动或java类中
语法:
public void closeActivity(View v){
Intent intent=new Intent(currentclass.this,statingclass.class);
startActivity(intent);
finish();
【讨论】:
也许您真正需要的是“向上”按钮而不是关闭按钮。来自官方 Android 文档:“您的应用程序应该让用户轻松找到返回应用程序主屏幕的方式。一种简单的方法是在应用程序栏上为除主要活动之外的所有活动提供一个向上按钮。 "
详细说明如下:https://developer.android.com/training/appbar/up-action.html
【讨论】: