Java 框架布局中另一个下方的Android视图不工作
我有这样的代码:Java 框架布局中另一个下方的Android视图不工作,java,android,android-layout,Java,Android,Android Layout,我有这样的代码: <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto&q
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:padding="15dp"
android:background="@drawable/button"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#FFF" />
<pl.jawegiel.endlessblow.other.GameSurface
android:id="@+id/gameSurface"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
(...)
</RelativeLayout>
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/left_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#22FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/right_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#99FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.drawerlayout.widget.DrawerLayout>
(...)
问题是我的按钮不在游戏表面之下
我把这个按钮作为第一个,指望它会低于游戏表面,但它不工作
如何实现这一点?您需要将FrameLayout更改为另一种视图组。FrameLayout是一种视图组类型,其中一个视图位于另一个视图之上。就像蛋糕上的一层层 在FrameLayout中,无法将一个放置在另一个之下 最简单的解决方案可能是使用LinearLayout更改FrameLayout,当然,记住向其添加android:orientation属性
注意:我建议您学习ConstraintLayout,它可以替换几乎所有类型的嵌套视图组,包括FrameLayout和RelativeLayout:-)这是使用
FrameLayout
无法实现的,因为FrameLayout通常只用于显示一个子视图。即使它可以显示更多的子视图,它们也只是放在彼此的顶部。上面的答案对此进行了解释。每个视图组都有自己的优缺点,最好的布局是线性布局。基于您可以使用的android:orientation
属性,LinearLayout可以将您孩子的一个放置在另一个下面或一个挨着另一个。值为垂直
和水平
。你知道这里应该用哪一个
因此,您需要做的只是将FrameLayout
更改为LinearLayout
。这就是它在我的XML中的外观:
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
然后我得到这个:
为什么不使用LinearLayout而不是FrameLayout?只需将其与垂直方向一起使用,并根据需要从上到下添加孩子。如果我更改为LinearLayout,则android:layout\u gravity=“center\u vertical”不起作用,并且按钮仍不在我想要实现的游戏表面下方。但看看这个例子:我们看到框架布局是我应该在这个例子中应用的,因为例子中的文本视图成功地放在ImageView的顶部。@Kiubaz TextView位于ImageView的顶部,但不在下面。您希望将按钮放置在ImageView下方。使用LinearLayout并设置方向:垂直。然后使用android:gravity=“center\u vertical”将其设置到所需位置。在这种情况下,您需要将按钮放置在XML内的GameSurface下方,并且LinearLayout还需要将layout_height=“match_parent”@Kiubaz更改为LinearLayout,将方向、更改顺序-按钮设置为最后一个子项。
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="@+id/gameSurface"
android:src="@drawable/ic_user"
android:layout_width="200dp"
android:layout_gravity="center_horizontal"
android:layout_height="200dp" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:layout_marginTop="20dp"
android:padding="15dp"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#000" />
</LinearLayout>