繁体   English   中英

Android对话框活动删除边距并在外面触摸时完成

[英]Android dialog activity remove margin and finish when touched outside

我有一个对话主题活动声明如下:

AndroidManifest:

<activity android:name=".DialogActivity" android:theme="@style/mydialog"></activity>

样式:

<style name="mydialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

关于DialogActivity的OnCreate:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);

int width = (int)(getResources().getDisplayMetrics().widthPixels*0.95);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.80);

getWindow().setLayout(width, height);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...

问题是当我触摸此图像中显示的红色矩形区域时活动未关闭:

在此输入图像描述

所以我的问题是如何删除这个额外的空间,以便活动在触及其实际形状之外时完成?

如果我在红色矩形外面触摸,活动就会很好。 已经尝试过这个 ,无法消除额外的空间。

我正在寻找相同的并最终得到这个解决方案:

Style.xml:

<style name="CustomTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:statusBarColor">@color/black</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

的Manifest.xml:

    <activity android:name=".CustomActivity"
        android:theme="@style/CustomTheme" // this is important
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize" />

activity_custom.xml:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#FFFFFF"
    android:theme="@style/CustomTheme" // this is important
    android:excludeFromRecents="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textColor="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

CustomActivity.kt:

class CustomActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom)

        // this is important
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }
}

我唯一没有找到如何通过外部点击关闭活动的解决方案。

如果是这样,将更新答案。

这对我有用

<style name="DialogThemeActivity" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item><!-- if required you can remove title also -->
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM