Android: RelativeLayout inside FrameLayout, width height 错误

标签 android android-layout android-fragments android-relativelayout android-framelayout

我有什么:

activity_comics.xml - 两个 fragment 的布局容器:漫画列表 (view_comicses.xml) 和详细漫画 View

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:id="@+id/comics_fragment_container">
</FrameLayout>

view_comicses.xml - 带有交错 ListView 的 fragment

<com.agimind.widget.huewu.pla.lib.MultiColumnListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:pla="http://schemas.android.com/apk/res-auto"
    android:id="@+id/multicollist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    pla:plaColumnNumber="2"
    pla:plaLandscapeColumnNumber="3">
</com.agimind.widget.huewu.pla.lib.MultiColumnListView>

view_comics_detail.xml - 漫画详细 View 的 fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/comics_image"
        android:layout_centerInParent="true" />

</RelativeLayout>

view_comics.xmlview_comics_detail.xml fragment 添加到activity_comics.xml后,生成的布局应该是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:id="@+id/comics_fragment_container">

    <com.agimind.widget.huewu.pla.lib.MultiColumnListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:pla="http://schemas.android.com/apk/res-auto"
        android:id="@+id/multicollist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        pla:plaColumnNumber="2"
        pla:plaLandscapeColumnNumber="3">
    </com.agimind.widget.huewu.pla.lib.MultiColumnListView>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/comics_image"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</FrameLayout>

但由于某些原因导致的布局看起来像这样

resulting layout

RelativeLayout 的宽度和高度等于 match_parent,但在渲染时就好像等于 wrap_content。为什么会这样?我尝试了 FrameLayoutRelativeLayout 的不同参数,但没有用,我们将不胜感激!

最佳答案

RelativeLayout 试图匹配 FrameLayout 的宽度但不能,因为 MultiColumnListView 在那里。然后 FrameLayout 将它的可用空间分配给两个 child 。

如果您希望 RelativeLayout 和 MultiColumnListView 都具有与 FrameLayout 相同的宽度(并且重叠),则需要将 FrameLayout 更改为 RelativeLayout。

FrameLayout更多的是用来展示单个child。可以添加多个 child ,但您需要为每个 child 指定 android:layout_gravity 以控制他们的位置。

关于Android: RelativeLayout inside FrameLayout, width height 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349430/

相关文章:

android - 导航组件 : Navigate from Fragment in Viewpager to another Fragment

android - onProviderEnabled 是否仅在为该提供者注册了监听器时才调用?

Android Studio 1.2.1.1布局预览问题

android - 如何在显示结果之前设置进度条?

android - 将动画翻译到 Android 中的正确位置

java - FragmentStatePagerAdapter 不处理更改

java - 如何在加载新的android fragment 时显示进度条?

java - Android:无法处理 UnknownHostException [使用 Retrofit 2、okhttp3、rxandroid 2]

android - 如何在appBar中添加一个图标? - flutter

android - Parcelable vs 从数据库读取的效率