簡體   English   中英

從全屏活動返回時,UI向下移動。 怎么不顯示這個?

[英]When returning from a fullscreen activity, the UI moves down. How to not show this?

MainActivity啟動FullActivity(沒有狀態欄)。 當用戶按下FullActivity中的后退按鈕時,將取消FullActivity並再次顯示MainActivity。

問題是當MainActivity再次顯示時,好像MainActivity正在從全屏轉換到正常屏幕。 整個UI向下移動狀態欄的高度。 這看起來很煩人。 我從未將MainActivity視為全屏。 為什么會發生這種情況,我該如何防止這種情況發生?

簡而言之,我希望MainActivity保持正常,永遠不會全屏。

在此輸入圖像描述

我嘗試將以下代碼添加到MainActivity的onCreate中,根據對類似問題的回答,但它不起作用

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

我在Nexus 7(2013),Android 6.0.1上進行了測試。

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button full = (Button)findViewById(R.id.full);
        full.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(MainActivity.this, FullActivity.class);
                startActivity(i);
            }
        });
    }
}

FullActivity.java

public class FullActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        int flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setFlags(flags, flags);
        TextView hello = new TextView(this);
        hello.setText("Press the back button to dismiss me.");
        setContentView(hello);
    }
}

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.me.test2"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".FullActivity"/>
    </application>

</manifest>

activity_main.xml中

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:text="Show full screen activity"
        android:id="@+id/full"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ListView
        android:entries="@array/list_items"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="1dp"></ListView>
</LinearLayout>

strings.xml中

<resources>
    <string name="app_name">Test2</string>
    <string-array name="list_items">
        <item>Homer</item>
        <item>Marge</item>
        <item>Bart</item>
        <item>Lisa</item>
        <item>Maggie</item>
        <item>Santa\'s Little Helper</item>
        <item>Snowball II</item>
        <item>Flanders</item>
        <item>Moe</item>
    </string-array>
</resources>

我相信你應該在你的FullScreen活動中覆蓋onBackPressed方法:

    @Override
    public void onBackPressed() {
//Clear the Flag fullscreen flag before finishing the activity
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
       overridePendingTransition(0,0); //(0, 0) value is used for suppressing the animation while transitioning from one activity to another
       finish(); //For finishing the current activity. You can even setResult and use finishActivity(requestCode) if you have started the current Activity for some result
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM