我无法在框架布局中移动按钮

5
我尝试使用图形界面和XML文件中的android:layout_alignParentLeft来移动按钮,但无论如何都不起作用。我的Android Studio版本是2.2.3。你是否遇到过这个问题?

enter image description here


请展示您的 XML 代码。 - Dileep Patel
2个回答

9

1
@MMManuel,我很高兴能够帮到你,祝你编程愉快 :) - Pavneet_Singh
那么我们为什么要使用FrameLayout而不是始终使用RelativeLayout呢?在大多数情况下,FrameLayout似乎是无用的。只有在需要整个区域的webView中才有用。 - Black
@Black 在运行时进行片段转换非常有用,特别是当您有多个视图重叠在一起并且只想显示顶部的一个视图时,或者像您想要在图像顶部显示描述视图等情况下。 - Pavneet_Singh

4

如果你被迫使用FrameLayout(例如在工具栏中),并且只有一个元素(或很少的元素)需要操作(如按钮),那么你可以使用android:layout_gravity=""属性来对齐FrameLayout中的元素。

   <FrameLayout
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
    </FrameLayout>

如果你的布局中只有很少的元素,可以采取以下两种方法:1)将FrameLayout改为RelativeLayout;2)将所有项包装在RelativeLayout中,并将参数设置为match_parent

    <FrameLayout
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="@string/app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"/>
    </RelativeLayout>
    </FrameLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接