【问题标题】:The Relative Layout doesn't cover the width of the screen相对布局不覆盖屏幕宽度
【发布时间】:2019-09-21 19:39:16
【问题描述】:

我正在制作一个使用相对布局在屏幕上创建透明黑色活动的应用程序,但它创建的布局不适合我的手机屏幕宽度。

这是我的编码结果

这是我的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A6000000"
        >

    </RelativeLayout>
</RelativeLayout>

这是我用来创建透明活动的 styles.xml

<style name="Theme.Transparent" parent = "android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

我已经为宽度和高度设置了 match_parent。谢谢。

【问题讨论】:

  • 请提供设备详细信息
  • 华为 vns-l22 API 级别 23
  • 上面的布局是Fragment还是Activity?
  • 试试 android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:alignParentBottom="true"
  • 这是一个布局优于布局(Relative)的透明活动

标签: java android layout android-relativelayout


【解决方案1】:

用这个改变style.xml。

 <style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">#33000000</item> 
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

在清单中

<activity
        android:theme="@style/Theme.AppCompat.Translucent"
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

【讨论】:

    【解决方案2】:

    您需要在您的styles.xml 中为您的Theme.Transparent 设置此项:

    <item name="android:windowIsFloating">false</item>
    

    浮动窗口通常是一个对话框,因此两边都有自动间距。将此设置为 false 将删除此自动间距。你可以阅读更多关于它的信息in the related documentation,具体来说:

    ...此窗口是否以浮动样式显示(基于样式/主题中的R.attr.windowIsFloating 属性)。

    不过,如果您正在制作全屏 RelativeLayout,您会想知道为什么您毕竟需要透明度......!

    【讨论】:

      【解决方案3】:

      我将使用 Constraintlayout 进行单父多视图

      <?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"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >
      
          <View
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#A6000000"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent">
      
          </View>
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 这并不能解决OP的问题,相对布局已经使用了最大可用空间。
      猜你喜欢
      • 1970-01-01
      • 2020-02-07
      • 2011-08-30
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多