gpt4 book ai didi

android - 使用 RelativeLayout 对齐 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:11 26 4
gpt4 key购买 nike

我在尝试将 fragment 与 RelativeLayout 对齐时遇到了真正的问题,尽管看起来这应该很简单。我只需要两个彼此相邻的 fragment ,如果我使用 LinearLayout 它工作正常:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<fragment android:name="com.fragment.test.TitlesFragment"
android:id="@+id/fragmentTitles"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>

<FrameLayout
android:id="@+id/details"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="fill_parent"
/>

</LinearLayout>

enter image description here

但是,如果我使用 RelativeLayout,则不会显示任何内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<fragment android:name="com.fragment.test.TitlesFragment"
android:id="@+id/fragmentTitles"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>

<FrameLayout
android:id="@+id/details"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/fragmentTitles"
/>

</RelativeLayout>

enter image description here

更新:

这是我看到的截图:

enter image description here

这是我正在使用的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:weightSum="3"
>
<fragment android:name="com.fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_above="@id/statusUpdated"
/>

<fragment android:name="com.fragment2"
android:id="@+id/fragment2"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_above="@id/statusUpdated"
android:layout_toRightOf="@id/fragment1"
/>

</LinearLayout>

<TextView android:id="@+id/statusUpdated" style="@style/Status" />

</RelativeLayout>

最佳答案

您不能在 RelativeLayout 中使用权重。基本上该属性会被忽略,这意味着您的两个 fragment 都以 0 的宽度呈现,因此它们是不可见的。

我不确定你想要完成什么,但你可能想考虑将你的第一个示例(LinearLayout)包装到 RelativeLayout - 在其他单词:组合/整合两种布局。不过,这确实会在您的 View 层次结构中产生另一个级别。


编辑:

我还没有真正测试过它,但我想这就是你要找的东西:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:id="@+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/footer_view"
android:weightSum="3">

<fragment
android:id="@+id/fragmentTitles"
android:name="com.fragment.test.TitlesFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />

<FrameLayout
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2" />
</LinearLayout>

<TextView
android:id="@+id/footer_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#f00"
android:text="I am a footer" />

</RelativeLayout>

显然,您可以将 TextView 替换为您喜欢的任何内容。

关于android - 使用 RelativeLayout 对齐 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10202060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com