![](/img/trans.png)
[英]How to center a button at the bottom of a FrameLayout in a RelativeLayout
[英]How to center an FrameLayout in a RelativeLayout
我的布局存在问题,如果一些Android专家可以检查并帮助我,那将是很好的:)
我有一个FragmentA,其中包含另一个FragmentB。
FragmentA包含3个FrameLayouts:
FragmentB还包含1个FrameLayout:
frameLayout.addView(new ImageClass(id);
问题是图像没有在中间水平居中。
这是第一个fragmentA的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/frame2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/frame1" />
<FrameLayout
android:id="@+id/frame3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
这是fragmentB的布局文件(包含在ID为frame2的FragmentA-> FrameLayout 2中)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageClass"
android:background="#0000FF"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
并使用以下代码通过画布将图像插入视图中:
Bitmap image= BitmapFactory.decodeResource(getResources(), id);
canvas.drawBitmap(image, 0, 0, null);
我以为RelativeLayout
中的android:gravity="center_horizontal"
和FragmentA
和FragmentB
两个FrameLayouts
中的android:layout_width="wrap_content"
都应将图像居中。 但这是在左侧。
屏幕快照有两个(第一幅)和外观(第二幅):
对不起,链接,但我无法发布图片(信誉不足)
RelativeLayouts处理重力的方式与您预期的不同( 链接 ):
请注意,由于RelativeLayout认为每个孩子相对于彼此的位置都很重要,因此设置重力会影响所有孩子作为父元素内单个单元的位置。 这种情况发生在孩子的位置相对较高之后。
一个快速的解决方法是将frame2从FrameLayout更改为RelativeLayout,添加match_parent宽度,然后将您的中心重力放在该位置上。
<RelativeLayout
android:id="@+id/frame2"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame1"/>
根据视图的要求,您可以采用多种其他方法。 通常最好避免嵌套RelativeLayouts,但在您的情况下可能有必要。
这里可能重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.