【发布时间】: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 的内容是位于屏幕每个边缘上方的叠加层。我不知道有任何其他方法可以做到这一点。