【问题标题】:How can I align an element to the right in the FrameLayout?如何在 FrameLayout 中将元素右对齐?
【发布时间】:2011-08-28 23:25:17
【问题描述】:

我有一个 FrameLayout,其中包含 2 个视图,第二个类似于 Close Button (X),我想将其放置在右侧。

我试过 layout_gravity = " right ",但没用。

这是我的布局 xml:

<FrameLayout android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:id="@+id/layoutSmallImg">

    <ImageView android:id="@+id/bigImage"
        android:layout_width="320dp" android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/bigImage"
        android:layout_width="320dp" android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/closebutton"
        android:background="@android:color/transparent"
        android:layout_height="30dp" android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_width="30dp" android:paddingLeft="40dp"
        android:visibility="gone" 
        android:src="@drawable/close" />
</FrameLayout>

【问题讨论】:

    标签: android android-xml android-framelayout


    【解决方案1】:

    我建议您改用RelativeLayout

    FrameLayout 的设计目的是在屏幕上屏蔽一个区域以显示单个项目。您可以将多个子项添加到 FrameLayout 并使用重力控制它们在 FrameLayout 中的位置。孩子们被画在一个堆栈中,最近添加的孩子在最上面。框架布局的大小是其最大子项(加上填充)的大小,可见与否(如果 FrameLayout 的父项允许)。仅当 setConsiderGoneChildrenWhenMeasuring() 设置为 true 时,才会使用 GONE 的视图来调整大小。

    顺便说一句,你想要 ImageView 顶部的按钮还是旁边的按钮?您将布局和 imageView 的宽度设置为相同的大小。


    评论后,我可以看到两个选项:

    【讨论】:

    • 感谢您的回答,我知道使用 relativeLayout 是可能的,我想在 FrameLayout 中对齐它,:( 是的,我希望 imageButton 位于 imageView 的顶部 | 右侧,它看起来像 Close我的图片按钮,当我点击时,图片会淡出
    • 感谢您抽出宝贵时间,我想我会再次使用 RelativeLayout ,但我对 FrameLayout 并不满意 :)
    【解决方案2】:

    阅读文档:

    FrameLayout 的设计目的是在屏幕上屏蔽一个区域以显示 单个项目。一般情况下,应该使用 FrameLayout 来容纳单个 子视图,因为在一个子视图中组织子视图可能很困难 在没有孩子的情况下可扩展到不同屏幕尺寸的方式 相互重叠。但是,您可以将多个孩子添加到 FrameLayout 并通过以下方式控制它们在 FrameLayout 中的位置 使用 android:layout_gravity 为每个孩子分配重力 属性。

    试试:

    <TextView
         .....
         android:layout_gravity="right" />
    

    您可以在 FrameLayout 中的 9 个不同位置放置视图

    享受

    【讨论】:

    【解决方案3】:

    只需将您的元素放入 FrameLayout 到第二个 RelativeLayout。并使用 android:layout_alignParentRight="true" 作为要右对齐的元素。

    <FrameLayout android:layout_width="320dp" android:layout_height="wrap_content" 
                                          android:id="@+id/layoutSmallImg">
     <RelativeLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
        <ImageView android:id="@+id/bigImage"
                android:layout_width="320dp" 
                android:layout_height="wrap_content"/>
    
        <ImageButton android:id="@+id/closebutton"
                android:background="@android:color/transparent"
                android:layout_height="30dp" android:layout_marginTop="10dp"
                android:layout_alignParentRight="true"
                android:layout_width="30dp" android:paddingLeft="40dp"
                android:visibility="gone" 
                android:src="@drawable/close" />
      </RelativeLayout>
    </FrameLayout>
    

    【讨论】:

      【解决方案4】:

      请改用RelativeLayout

      【讨论】:

      • 感谢您的回答,我知道使用 relativeLayout 是可能的,我想在 FrameLayout 中对齐它,:(
      • 然后在视图中添加左边距
      【解决方案5】:

      以编程方式,您可以使用 FrameLayout.LayoutParams 设置边距。以下方法可以在屏幕底部放置一个视图:

      int desired_height_of_content = //whatever your want the height of your view to be
      FrameLayout layout = new FrameLayout(this);
      FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, desired_height_of_content);
      int difference = screen_height - desired_height_of_content
      flp.setMargins(difference, 0, 0, 0);
      layout.addView(your_view, flp);
      

      【讨论】:

        【解决方案6】:

        你想使用 FrameLayout 还是 RelativeLayout?

        我的理解是,如果您想用另一个视图替换当前视图,则使用 FrameLayout。 RelativeLayout 可以满足您的要求。

        【讨论】:

        • 感谢您的回复我想使用 FrameLayout,b 默认,FrameLAyout 位置在 frameLayout 的左上角视图,我想这样做,但在右上角,
        • 您也可以使用 FrameLayout 实现您想要的。将 ImageButton 的 layout_gravity 设置为“right”。 layout_alignParentRight 适用于RelativeLayout,我不知道它是否也适用于FrameLayout。其他任何人,请说明这一点。
        • 谢谢,但我已经尝试过 layout_gravity="right" 但它不起作用:)
        • 我实际上复制了您的代码,只是删除了 android:visibility="gone" 这一行。我添加了 layout_gravity="right"。它对我来说非常好。只是我手机的屏幕尺寸不是很大,所以我得到了一个半切的图像,但我确实把它对齐了,所以它工作正常:)
        • 是的,我知道,我看到我的逻辑足以将 ImageButton 向右对齐,但最后,我使用了 RelativeLayout,它现在工作的基本要素 :) 谢谢为你的时间;)
        猜你喜欢
        • 1970-01-01
        • 2014-01-10
        • 2011-12-03
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        相关资源
        最近更新 更多