android - 为全屏 Activity 摆脱不必要的根布局

如何摆脱不必要的全屏 Activity 的根布局?

我有一个简单的全屏 Activity 。布局检查器显示了 Android 提供的大量根布局层次结构,我在全屏时不需要这些层次结构。

我可以摆脱它们吗?

  • 如果是,怎么做?
  • 如果没有,为什么不呢?

测试设置

打开 Android Studio。创建一个新项目。接受默认设置。选择“空 Activity ”。你会得到这个 list :

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

你会得到这个layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blcknx.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

调用 Tools > Android > Layout Inspector 以查看生成的根模板。

要使其全屏,只需在布局中的 TextView 中添加一个 id HelloWorld 并更新 MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView layout = findViewById(R.id.HelloWorld);
        layout.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LOW_PROFILE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        );
    }
}

标志隐藏了导航,但它们不会删除未使用的布局。

你的答案

使用简单的 TextView。全屏显示。通过 Tools > Android > Layout Inspector 检查您删除了根布局的程度。显示屏幕截图。显示您的代码。

最佳答案

您可以摆脱 action_bar_container 将 Activity 的主题从 DarkActionBar 更改为 NoActionBar:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
</style>

然后你会得到以下树:

如果您想更进一步并摆脱 ContentFrameLayout,那么您可以这样做:


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val contentView = findViewById(android.R.id.content)
        val parent = contentView.parent as ViewGroup
        parent.removeView(contentView)
        LayoutInflater.from(this).inflate(R.layout.activity_main, parent)
    }

这将是 View 层次结构树:

不确定是否应该删除 android.R.id.content View ,可能有些库假设应该存在这样的 View 。

至于action_mode_bar_stub:你不应该关心它们,只要它们是ViewStub s,这不会影响整体性能,因为它们会延迟膨胀到 View 层次结构中。

https://stackoverflow.com/questions/49054406/

相关文章:

android - 如何在 kotlin 协程上进行指数退避重试

javafx - Kotlin 。基本 JavaFX 应用程序

android - 应用程序模块中引用的 android 库模块的“ Unresolved 引用”错

android - Kotlin 属性访问语法如何适用于 Java 类(即 EditText set

kotlin - 比较字符串 Kotlin

android - 如何混淆我用 kotlin 编码的 sdk(并摆脱元数据)

spring - kotlin 和 @Valid Spring 注解

android - kotlin 'onCreate' 不会覆盖任何内容

android - "Extension with name ' android ' does no

enums - 如何在 Kotlin 中声明枚举类型的变量?