【问题标题】:How to have activity in Android with a close button at the top corner?如何通过右上角的关闭按钮在 Android 中进行活动?
【发布时间】:2017-08-03 04:02:09
【问题描述】:

我想要一个包含关闭按钮的活动,即右上角的“x”关闭活动。任何这方面的例子都会非常有帮助。

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    在清单中创建一个带有透明主题的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 背景设置为您想要的任何颜色时,您会看到它位于屏幕的中心
    • 您不应将“sp”用于宽度和高度等尺寸。它们仅用于文本。而是使用“dp”。
    【解决方案2】:

    使用 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();
    
            }
        });
    

    【讨论】:

      【解决方案3】:

      在你的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();
      

      【讨论】:

        【解决方案4】:

        也许您真正需要的是“向上”按钮而不是关闭按钮。来自官方 Android 文档:“您的应用程序应该让用户轻松找到返回应用程序主屏幕的方式。一种简单的方法是在应用程序栏上为除主要活动之外的所有活动提供一个向上按钮。 "

        详细说明如下:https://developer.android.com/training/appbar/up-action.html

        【讨论】:

        • 问题是 5 岁。那时还没有 Action Bar。请把注意力集中在最近和未回答的问题上。谢谢。
        • 为什么我不应该为现在可能相关的旧问题提供最新答案?至少我认为这不值得投反对票??
        • 向上按钮是当今的标准。希望问题中所问内容的人不会对向上按钮感到满意,而是会遵循已接受的答案。这使得答案立即过时。这篇文章本可以被总结为评论。
        猜你喜欢
        • 2021-06-09
        • 1970-01-01
        • 2020-02-08
        • 1970-01-01
        • 2013-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多