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

时间:2016-12-28 20:37:15

标签: android android-layout

我有一个如下所示的对话主题活动:

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);
...

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

enter image description here

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

如果我在红色矩形外面触摸,活动就会完成。已经尝试this,无法删除多余的空间。

2 个答案:

答案 0 :(得分:0)

我一直在寻找相同的东西,最终得到了这个解决方案:

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)
    }
}

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

如果愿意,将更新答案。

答案 1 :(得分:-1)

这对我有用

<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>