【问题标题】:RelativeLayout takes fullscreen with layout_height="80dp"RelativeLayout 采用 layout_height="80dp" 全屏
【发布时间】:2016-03-26 17:49:47
【问题描述】:

我正在尝试创建一个高度为 80dp 的片段,在设计部分它似乎工作得很好,但是当我在我的设备上运行应用程序时,RelativeLayout 不知何故需要全屏。我对 SeekBar 使用 android:layout_alignParentBottom="true",但据我所知,如果 Layout 的高度不是 wrap_content,它不应该全屏显示。这是 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#bbbbff">

    <SeekBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/playButton"/>

    <Button
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:text="play"
        android:layout_alignParentRight="true"
        android:id="@+id/playButton"/>
</RelativeLayout>

编辑 1:我只是尝试将此布局用于活动,它不再需要全屏,但我仍然遇到片段问题。另外,我不会以编程方式更改 Layout 的高度。

编辑2:我使用fragmentTransaction.add(R.id.musicBarContainer, musicProgressBar) 将片段添加到活动中,其中musicProgressBar 是片段java 类的一个实例。 musicBarContainer XML 代码是

<FrameLayout
        android:id="@+id/musicBarContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
    </FrameLayout>

在 onCreateView 我使用View view = inflater.inflate(R.layout.music_progress_bar, null);

【问题讨论】:

  • xml 就好了。您是否正在以编程方式更改某些内容,例如更改高度运行时间? @Joose
  • @Joose 看看我的回答。
  • 怎么称呼你的片段和片段布局?
  • @Ozlem,请查看 EDIT 2
  • 我把你的代码sn-ps写到一个空的项目中,我没有遇到你的问题,即出现fragment的情况下布局不是全屏。也许你错过了代码中的一点;你能写下你的整个代码,以便我们可以正确地检查它吗?

标签: android xml android-layout


【解决方案1】:

来自RelativeLayout 文档:

类概述

一种布局,其中子项的位置可以相对于彼此或与父项进行描述。

请注意,RelativeLayout 的大小与其子项的位置之间不能存在循环依赖关系。 例如,您不能拥有高度设置为 WRAP_CONTENT 且子级设置为 ALIGN_PARENT_BOTTOM 的 RelativeLayout

Class documentation

这正是你的情况。 RelativeLayout 做不到。

有关更多详细信息,请访问此答案:RelativeLayout is taking fullscreen for wrap_content

【讨论】:

  • 我猜不是这样,因为布局的高度在这里不是相对的,它被严格定义为80dp。我错了吗?
【解决方案2】:

在 MusicProgressBar 片段类中,而不是

        View view = inflater.inflate(R.layout.music_progress_bar, null);        

你应该写

        View view = inflater.inflate(R.layout.music_progress_bar, container, false);

【讨论】:

    【解决方案3】:

    尝试将搜索栏的高度设置为 20dp 并检查。我认为它的 wrap_content 以某种方式覆盖了父母的高度参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      相关资源
      最近更新 更多