FrameLayout边距不起作用

Vin*_*oth 76 android android-layout

我的布局结构是这样的

LinearLayout
    FrameLayout
       ImageView
       ImageView
    FrameLayout
    TextView
LinearLayout
Run Code Online (Sandbox Code Playgroud)

我为FrameLayout中的两个ImageView设置了margin.但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>
Run Code Online (Sandbox Code Playgroud)

小智 220

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

  • 它有效,但它让我觉得很脏.我以为我已经逃过了CSS黑客的日子...... (21认同)
  • 为了更明确的原因.FrameLayout.onLayout()调用包含它(至少在api v2.3.4中):`//为每个子节点:final LayoutParams lp =(LayoutParams)child.getLayoutParams(); final int gravity = lp.gravity; if(gravity!= -1){//处理所有与保证金相关的东西`因此,如果重力为-1,则不会计算保证金.FrameLayout.LayoutParams中的重力定义为:`gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity,-1); `因此,如果未设置重力,则不会计算保证金. (12认同)
  • 它似乎固定在4.1(可能在之前).虽然有用但知道 (2认同)

Mik*_*kab 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
Run Code Online (Sandbox Code Playgroud)

因此,如果重力为-1,则不会计算保证金.事实是,FrameLayout.LayoutParams中的引力定义为:

    gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);
Run Code Online (Sandbox Code Playgroud)

因此,如果未设置重力,则不会计算保证金.


Sav*_*dar 7

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

机器人:layout_gravity = "顶"

一切都好!

而且你没有像这样设置新的布局参数;

FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(WallpapersActivity.ScreenWidth/2, layH);
Run Code Online (Sandbox Code Playgroud)

使用这样:

FrameLayout.LayoutParams llp = (LayoutParams) myFrameLay.getLayoutParams();
llp.height = 100;
llp.width = 100;
myFrameLay.setLayoutParams(llp);
Run Code Online (Sandbox Code Playgroud)