Android 框架布局边距不工作
我的布局结构是这样的Android 框架布局边距不工作,android,android-layout,Android,Android Layout,我的布局结构是这样的 LinearLayout FrameLayout ImageView ImageView FrameLayout TextView LinearLayout 我已经为FrameLayout内的两个ImageView设置了边距。但是FrameLayout页边距被丢弃,它总是将图像设置在左上角。如果我从FrameLayout更改为LinearLayout,则页边距将正常工作。如何处理 <?xml version="1.
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:layout\u gravity吗?尝试在图像视图中使用android:padding而不是android:layout\u margin。AFAIK页边距在框架布局上无法正常工作。我甚至为此写过一次自定义布局。顺便说一句,您希望如何将ImageView全部注册?摘自FrameLayout文档()
框架布局的大小是其最大子级(加上填充)的大小,无论是否可见(如果框架布局的父级允许)
这似乎描述了这样一个事实,即它将剥离利润。就像boulder提到的,你可以尝试切换到填充,因为如果操作得当,它可以产生类似的效果
出于好奇,您提到使用
LinearLayout
容器时它确实可以正常工作,为什么选择FrameLayout?我自己也有同样的问题,并注意到设置布局
边距在您还设置ImageView的布局重力(即android:layout\u gravity=“top”)之前是不起作用的
在XML资源文件中,或从代码:FrameLayout.LayoutParams.gravity=gravity.TOP代码>将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);
尝试将setCroptoppadding(true)设置为ImageView,这应该会有所帮助 更清楚地说明原因。FrameLayout.onLayout()调用包含以下内容(至少在api v2.3.4中):
因此,如果重力为-1,则不会进行裕度计算。问题是,FrameLayout.LayoutParams中的重力定义如下:
gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);
因此,如果未设置重力,将不会计算边距。您必须在XML资源文件中设置ImageView的布局重力top,即android:layout\u gravity=“top”,或者从代码:FrameLayout.LayoutParams.gravity=gravity.topFramelayoututit中没有权重,但它会让我感觉脏。我以为我已经逃脱了CSS黑客的日子…FrameLayout.LayoutParams.gravity在2.3中没有gravity选项它似乎是固定在4.1上的(可能以前)。知道Though很有用,修复了我在旧kindle fire上遇到的一个问题。谢谢更清楚地说明原因。FrameLayout.onLayout()调用包含以下内容(至少在api v2.3.4中)://对于每个子级:final LayoutParams lp=(LayoutParams)child.getLayoutParams();最终整数重力=lp重力;如果(gravity!=-1){//处理所有与边距相关的内容
那么如果gravity为-1,将不会进行边距计算。FrameLayout.LayoutParams中的gravity由以下内容定义:gravity=a.getInt(com.android.internal.R.styleable.FrameLayout\u Layout\u gravity,-1)
因此,如果未设置重力,将不会计算边距。因此,如果没有LayoutParams,第二个方法将崩溃。您可以使用if==null控件进行检查。这解决了Android 10上边距的问题。
gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);