【问题标题】:Loading fragments in a RelativeLayout在 RelativeLayout 中加载片段
【发布时间】:2014-11-28 18:28:21
【问题描述】:

我在 FrameLayout 中有两个 RelativeLayout。这个想法是它们将占据屏幕的左右边缘,第一个视图(在本例中为文本视图)占据屏幕的中间部分。

在每个 RelativeLayout 中都会有一个复合按钮的垂直列表。每个按钮旁边都有一个片段,单击时会滑动打开。因此,单击屏幕左侧的按钮会导致选项列表滑到按钮右侧打开。单击屏幕右侧的按钮应该会导致选项列表滑动到按钮的左侧。

屏幕左侧的RelativeLayout 完美运行。点击按钮会使片段出现在按钮的右侧。但我无法让屏幕右侧的 RelativeLayout 正常运行。单击其中一个按钮时,其下方的所有按钮都会向左滑动。我似乎无法将按钮锚定到右边缘,因此片段总是出现在按钮的左侧,而其余按钮则位于屏幕的右边缘。

我做错了什么?有没有更好的方法来做到这一点?

片段附加到 Activity onCreate() 中的按钮。

这是没有点击按钮时的样子

单击左侧的按钮会正确加载片段。一切都固定在左边缘。

单击右侧的按钮是问题所在。按钮不会停留在右侧边缘,而是浮动到左侧。 GGG 的行为不同,因为(我认为)该片段尚未附加到 onClick 侦听器。

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:text="@string/hello_world"
        android:layout_gravity="center_vertical|center_horizontal" 
        android:background="#000000"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#33ffffff">

        <com.example.fragmentbuttonstest.widget.SelectionButton
            android:id="@+id/sb_aaa_selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/sb_aaa_frag_container"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@+id/sb_aaa_selection"/>

        <com.example.fragmentbuttonstest.widget.SelectionButton
            android:id="@+id/sb_bbb_selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sb_aaa_selection" />
        <FrameLayout
            android:id="@+id/sb_bbb_frag_container"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@+id/sb_bbb_selection"
            android:layout_below="@+id/sb_aaa_selection" />

        <com.example.fragmentbuttonstest.widget.SelectionButton
            android:id="@+id/sb_ccc_selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sb_bbb_selection" />
        <FrameLayout
            android:id="@+id/sb_ccc_frag_container"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@+id/sb_ccc_selection"
            android:layout_below="@+id/sb_bbb_selection"/>

    </RelativeLayout>

 <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#33ffffff"
        android:layout_gravity="top|right">

       <com.example.fragmentbuttonstest.widget.SelectionButton
            android:id="@+id/sb_ddd_selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/sb_ddd_frag_container" /> 
      <FrameLayout
            android:id="@+id/sb_ddd_frag_container"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>


      <com.example.fragmentbuttonstest.widget.SelectionButton
            android:id="@+id/sb_eee_selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sb_ddd_selection"
            android:layout_toRightOf="@+id/sb_eee_frag_container"           />
       <FrameLayout
            android:id="@+id/sb_eee_frag_container"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/sb_ddd_selection" />


      <com.example.fragmentbuttonstest.widget.SelectionButton
            android:id="@+id/sb_fff_selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sb_eee_selection"
            android:layout_toRightOf="@+id/sb_ddd_frag_container" />
      <FrameLayout
            android:id="@+id/sb_fff_frag_container"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/sb_eee_selection" />

    </RelativeLayout>
</FrameLayout>

【问题讨论】:

  • 请发布您期望的 xml 设计和当前视图
  • "我在 FrameLayout 中有两个 RelativeLayouts。" - framelayout 只能容纳一项。
  • @GoranHoriaMihail 我认为在大多数情况下这是一个很好的规则,但不是必需的。 ...如果我正确阅读 API 指南。在这种情况下,我希望第一个 View 占据整个屏幕,RelativeLayouts 的内容是位于屏幕每个边缘上方的叠加层。我不知道有任何其他方法可以做到这一点。

标签: android android-layout


【解决方案1】:

看来我不会得到回复,所以我将分享我想出的解决方案......

我对必须手动定位每个元素一点也不满意,但是(到目前为止)我还无法通过在布局中包装按钮和片段来正确对齐所有元素。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="#000000"
        android:text="@string/hello_world"
        android:textAlignment="center" />

    <!-- Left side button bar -->

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#33ffffff" />

    <!-- AAA Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_aaa_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/sb_aaa_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp" />

    <!-- BBB Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_bbb_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp" />

    <FrameLayout
        android:id="@+id/sb_bbb_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="45dp" />

    <!-- CCC Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_ccc_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp" />

    <FrameLayout
        android:id="@+id/sb_ccc_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="90dp" />

    <!-- DDD Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_ddd_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="135dp" />

    <FrameLayout
        android:id="@+id/sb_ddd_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="135dp" />

    <!-- EEE Position Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_eee_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp" />

    <FrameLayout
        android:id="@+id/sb_eee_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="180dp" />



    <!-- Right Side Button Bar -->
    <!-- Right Side Background -->

    <RelativeLayout
        android:layout_width="45dp"
        android:layout_height="match_parent"
        android:layout_gravity="top|right"
        android:background="#33ffffff" />

    <!-- FFF Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_fff_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right" />

    <FrameLayout
        android:id="@+id/sb_fff_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="45dp" />

    <!-- GGG Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_ggg_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="45dp" />

    <FrameLayout
        android:id="@+id/sb_ggg_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="45dp"
        android:layout_marginTop="45dp" />

    <!-- HHH Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_hhh_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="90dp" />

    <FrameLayout
        android:id="@+id/sb_hhh_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="45dp"
        android:layout_marginTop="45dp" />

    <!-- III Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_iii_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="135dp"
        android:layout_toRightOf="@+id/sb_player_frag_container" />

    <FrameLayout
        android:id="@+id/sb_iii_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="45dp"
        android:layout_marginTop="90dp" />

    <!-- JJJ Selection -->

    <com.example.fragmentbuttonstest.widget.SelectionButton
        android:id="@+id/sb_jjj_selection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="180dp" />

    <FrameLayout
        android:id="@+id/sb_jjj_frag_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="45dp"
        android:layout_marginTop="135dp" />

</FrameLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多