2011-03-23 61 views
73

我的布局结构是这样的的FrameLayout保证金不工作

LinearLayout 
    FrameLayout 
     ImageView 
     ImageView 
    FrameLayout 
    TextView 
LinearLayout 

我已经设置保证金的两个ImageView的这是内部的FrameLayout。但是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> 

回答

215

我有同样的问题我自己,发现设置layout_利润率不工作,直到您还可以设置您的ImageView布局重心即android:layout_gravity="top"在XML资源文件,或代码:FrameLayout.LayoutParams.gravity = Gravity.TOP;

+19

它的作品,但它让我感到肮脏。我以为我逃过了CSS黑客的日子...... – erlando 2012-04-30 13:38:47

+1

它的工作原理,想知道它是否是一个错误? – Johnny 2012-06-27 18:07:53

+1

FrameLayout.LayoutParams.gravity在2.3中没有重力选项 – JPM 2012-07-13 17:13:49

0

你试过android:layout_gravity吗?尝试在你的ImageViews中使用android:padding而不是android:layout_margin。 AFAIK边距在帧布局上无法正常工作。我甚至不得不为此编写一次自定义布局。顺便说一句,你怎么想你的ImageViews allign你?

+0

没有重量在一个framelayout – njzk2 2011-11-25 11:48:45

1

从FrameLayout里文档(link)摘自

框架布局的大小是其最大的孩子(加垫)的大小,可见或不可见(如果FrameLayout里的父许可证)。

这似乎描述了它将剥离利润的事实。就像提到的巨石,你可以尝试切换到填充,因为如果正确完成,它可以用来产生类似的效果。

出于好奇,你提到它在使用容器时工作正常,为什么选择FrameLayout?

7

添加XML这个属性并重新运行

机器人:layout_gravity = “顶”

一切OK!

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

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); 
+1

所以,如果你没有LayoutParams,它会崩溃的第二种方法。 – 2014-02-27 00:12:03

+0

你可以用if == null控件来检查它。 – 2015-03-01 18:03:20

+0

这解决了我在Android 10上的margin_top问题。 – 2016-02-07 06:41:56

0

尝试setCropToPadding(真),以ImageView的,这应该帮助!

8

为了更清楚地说明原因。该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 = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1); 

所以,如果重心未设置,就没有裕度计算。

0

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