【问题标题】:Android: Unwanted White Box at Top of Screen (toolbar?)Android:屏幕顶部不需要的白框(工具栏?)
【发布时间】:2017-04-26 06:12:26
【问题描述】:

如何摆脱 android 视图顶部的白框?我在我的代码中看不到任何地方我调用了创建工具栏的东西,我也没有自己编码。它也存在于视图之上,而不是其中。我猜xml文件的设计视图中有一些设置可以切换它?提前致谢!

** 我还应该包括它只在这个活动上,而我的其他活动顶部没有这个白条。此外,actionbar 是白色汽车上方的蓝色细条,因此任何涉及操作 actionbar 的代码都不会改变白色条的状态。 **

编辑:XML 代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bucket"
    android:theme="@+id/BucketTheme2"
    android:backgroundTint="#70FFFFFF"
    android:backgroundTintMode="src_over">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow>
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingTop="100dp"
            android:text="Find a place to go around you!"
            android:textColor="#ff000000"
            android:textSize="18dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="randomButtonPressed"
            android:text="RANDOMIZE"
            android:textSize="20dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/bucketBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="bucketButtonPressed"
            android:text="Bucket List"
            android:textSize="20dp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/searchLocationBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="searchButtonPressed"
            android:text="Search by Location"
            android:textSize="20dp" />

    </TableRow>

</TableLayout>

</RelativeLayout>

编辑 2:BucketTheme2 的 style.xml 代码如下(我最初没有发布它,因为它只设置颜色):

<style name="BucketTheme2" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

【问题讨论】:

  • 分享你的布局代码
  • 发布您的 xml 代码
  • 删除android:backgroundTint="#70FFFFFF" android:backgroundTintMode="src_over"
  • 也分享你的styles.xml。
  • 你能上传style.xml吗?你用什么风格作为主题?我猜那是工具栏/应用栏/原色。

标签: android xml layout toolbar


【解决方案1】:

在 Manifest.xml 中为应用程序或活动设置主题以删除操作栏:

android:theme="@android:style/Theme.Holo.Light.NoActionBar"

或者在Activity的onCreate方法中添加如下代码并导入android.support.v7.app.ActionBar:

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

【讨论】:

  • getSupportActionBar( ).hide( ) 代码有效!你知道为什么会这样吗?
  • 所以对于其他人来说,我认为支持操作栏是否存在取决于该类是否实现ActivityAppCompatActivity。当我将类的实现更改为Activity 时,该栏消失了。
【解决方案2】:

添加以下主题到style.xml

 <style name="AppTheme.NoActionBar">
      <item name="windowActionBar">false</item>
      <item name="windowNoTitle">true</item>
 </style>

然后将此主题设置为manifest中的活动:

  <activity
      android:name=".MainActivity"
      android:theme="@style/AppTheme.NoActionBar"/>

更新:您还可以设置主题如下:

<style name="BucketTheme2" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

希望这会有所帮助。

【讨论】:

  • 不幸的是,该栏仍然存在:/刚刚删除了顶部的状态栏。
  • 这不应该删除状态栏。您是否删除了之前添加的代码?请分享BucketTheme2 代码。如果您在活动类中对主题进行了任何修改,也将其显示出来。
  • 我包含了 BucketTheme2 代码,但正如您所见,它只为主题设置颜色。
【解决方案3】:

将清单中的活动主题更改为Theme.AppCompat.Light.NoActionBar,您应该被排序

【讨论】:

  • 不幸的是,这使操作栏消失了,但白条仍然存在:/
【解决方案4】:

在 style.xml 中添加 NoActionBar 样式

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

并从清单中将此主题添加到您的活动中,

<activity android:name=".YourActivity"
        android:theme="@style/AppTheme"
        >
    </activity>

【讨论】:

  • 不幸的是只去掉了顶部的状态栏:/
  • @Kiley 我已经编辑了 sn-p 及其为我工作,检查一下,如果它帮助你请投票回答
  • 感谢您的帮助,但即使进行了编辑,我仍然得到相同的结果。
  • 你能分享你的 manifest.xml n style.xml 并确保你没有在 xml 中使用工具栏
  • 视图将其个人主题设置为BucketTheme2,因此清单代码不会影响这一点。我从style.xml 文件中发布了BucketTheme2 的代码,如您所见,它只设置颜色。
【解决方案5】:

试试

 requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);

应该在 setContentView 之前

【讨论】:

  • 这也会减少状态栏。
  • 那么只使用requestWindowFeature(Window.FEATURE_NO_TITLE);
  • 这段代码实际上去掉了顶部的状态栏,但白条还在:/
  • 尝试在设备上运行这段代码,如果你只使用第一行,标题栏应该被移除,通知栏仍然存在。如果不是这样,那么我需要查看您的样式文件
【解决方案6】:

按照你的风格试试这个

<style name="BucketTheme2" parent="android:Theme.Holo.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

【讨论】:

  • 不幸的是该栏仍然存在:/这只是删除了顶部的状态栏。
  • 能否提供启用布局边界的屏幕截图?
【解决方案7】:

我在使用 Android Studio 已经设置的模板项目时遇到了同样的问题。我注意到在 activity_main.xml 文件中,一旦我更改为 *.NoActionBar 的主题,顶部就会出现不必要的填充。如果您删除 android:paddingTop="?attr/actionBarSize" 它应该会消失。

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize"></androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多