【问题标题】:FrameLayout margin not workingFrameLayout边距不起作用
【发布时间】:2011-07-21 02:12:43
【问题描述】:

我的布局结构是这样的

LinearLayout
    FrameLayout
       ImageView
       ImageView
    FrameLayout
    TextView
LinearLayout

我已经为 FrameLayout 内的两个 ImageView 设置了边距。但是 FrameLayout 边距被丢弃,它总是将图像设置为左上角。如果我从 FrameLayout 更改为 LinearLayout,则边距可以正常工作。如何处理?

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/inner1"
    >
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        >

            <ImageView
             android:layout_width="24px" 
             android:layout_height="24px" 
             android:id="@+id/favicon"
             android:layout_marginLeft="50px"
             android:layout_marginTop="50px"
             android:layout_marginBottom="40px"
             android:layout_marginRight="70px"      

            />      
            <ImageView
             android:layout_width="52px" 
             android:layout_height="52px" 
             android:id="@+id/applefavicon" 
             android:layout_marginLeft="100px"
             android:layout_marginTop="100px"
             android:layout_marginBottom="100px"
             android:layout_marginRight="100px"              
            />

        </FrameLayout>  
            <TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/title"                 
            android:layout_marginLeft="10px"        
            android:layout_marginTop="20px"
            android:textColor="#FFFFFF"  
            android:textSize = "15px"
            android:singleLine = "true"
            />

    </LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您必须在 XML 资源文件中设置 ImageView 的布局重力顶部,即 android:layout_gravity="top",或者通过代码:FrameLayout.LayoutParams.gravity = Gravity.TOP

    【讨论】:

      【解决方案2】:

      我自己也遇到了同样的问题,并注意到设置 layout_ 边距不起作用,除非您还在 XML 资源文件中设置了 ImageView 的布局重力,即 android:layout_gravity="top",或者来自代码:FrameLayout.LayoutParams.gravity = Gravity.TOP;

      【讨论】:

      • 它有效,但它让我觉得很脏。我以为我已经摆脱了 CSS hack 的日子......
      • FrameLayout.LayoutParams.gravity 在 2.3 中没有重力选项
      • 它似乎已在 4.1 上修复(可能之前)。了解虽然有用
      • 修复了我在旧 kindle fire 上遇到的问题。谢谢!
      • 为了更清楚地说明原因。 FrameLayout.onLayout() 调用包含以下内容(至少在 api v2.3.4 中):// for each child: final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int gravity = lp.gravity; if (gravity != -1) { // handle all margin related stuff 因此,如果重力为 -1,则不会计算边距。 FrameLayout.LayoutParams中的Gravity定义为:gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1); 所以如果gravity没有设置,就不会计算margin。
      【解决方案3】:

      为了更清楚地说明原因。 FrameLayout.onLayout() 调用包含以下内容(至少在 api v2.3.4 中):

          // for each child
          final LayoutParams lp = (LayoutParams) child.getLayoutParams();
          final int gravity = lp.gravity;
          if (gravity != -1) {
              // handle all margin related stuff
      

      所以如果gravity是-1,就不会有margin计算。问题是,FrameLayout.LayoutParams 中的重力由以下方式定义:

          gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);
      

      所以如果没有设置gravity,就不会有margin计算。

      【讨论】:

        【解决方案4】:

        尝试 setCropToPadding(true) to ImageView ,应该会有帮助!

        【讨论】:

          【解决方案5】:

          你试过 android:layout_gravity 吗?尝试在 ImageViews 中使用 android:padding 而不是 android:layout_margin。 AFAIK 边距在框架布局上无法正常工作。我什至不得不为此目的编写自定义布局一次。顺便说一句,你想如何对齐你的 ImageViews?

          【讨论】:

          • 框架布局中没有权重
          【解决方案6】:

          添加你的xml这个属性并重新运行

          android:layout_gravity="top"

          一切正常!

          而且你不会像这样设置新的布局参数;

          FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(WallpapersActivity.ScreenWidth/2, layH);
          

          这样使用:

          FrameLayout.LayoutParams llp = (LayoutParams) myFrameLay.getLayoutParams();
          llp.height = 100;
          llp.width = 100;
          myFrameLay.setLayoutParams(llp);
          

          【讨论】:

          • 所以如果你没有LayoutParams,用第二种方法会崩溃。
          • 您可以使用 if == null 控件进行检查。
          • 这解决了我在 Android 10 上的 margin_top 问题。
          【解决方案7】:

          取自 FrameLayout 文档 (link)

          框架布局的大小是其最大子项(加上填充)的大小,可见与否(如果 FrameLayout 的父项允许)。

          这似乎描述了它会去除边距的事实。就像提到的巨石一样,您可以尝试切换到填充,因为如果操作正确,它可以用来产生类似的效果。

          出于好奇,您提到使用LinearLayout 容器时它确实可以正常工作,为什么选择FrameLayout?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-22
            • 2017-07-02
            相关资源
            最近更新 更多