![](/img/trans.png)
[英]Android: Set margins in a FrameLayout programmatically- not working
[英]Margins inside framelayout is not working in Android 2.2
我使用framelayout,如果我使用的话,里面有2个textview
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
我的布局在Android 2.2中显示不正确,而在Android 4.0中同样可以正常工作。
这是完整的代码。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="left|center"
android:text= "Testing String"
android:textSize="15sp"
android:textColor="@color/LIGHTGRAY"
android:layout_gravity="left|top"/>
<TextView
android:id="@+id/ID_due_date"
android:textStyle="bold"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text= "Testing String"
android:layout_marginTop="10dp"
android:layout_gravity="right"
android:gravity="left|center"/>
</FrameLayout>
</ScrollView>
在Android 2.2中,它看起来像这样
在Android 4.0 ICS中,它看起来像这样
Android 2.2(Froyo)和2.3(Gingerbread)中存在一个错误,其中FrameLayout
内的边距不适用于未设置layout_gravity
属性的孩子。 您可以在这里看到它: https : //code.google.com/p/android/issues/detail?id=28057
该错误已在Android 3.0(Honeycomb)中修复。
您的问题与此错误有关。 您确实已经设置了layout_gravity
属性,因此页边距已正确应用-视图从顶部和侧面偏移了10dp
。 不幸的是, FrameLayout
不知道如何测量此类子项,因此在没有这些边距的情况下计算其边界-这就是为什么在底部( 10dp
)切割子项的原因。
高度为match_parent
FrameLayout
不会受到此问题的影响,但是在您的情况下,您设置了wrap_content
行为。 ( 请注意 ,即使将高度设置为fill_parent
它仍将作为wrap_content
起作用,因为它位于ScrollView
)。
请尝试使用填充来代替-在大多数情况下(背景或可点击区域并不重要),边距可用填充来代替。
您可以使用padding:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:gravity="left|center"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="Testing String"
android:textColor="@color/LIGHTGRAY"
android:textSize="15sp" />
<TextView
android:id="@+id/ID_due_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="left|center"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="Testing String"
android:textSize="15sp"
android:textStyle="bold" />
</FrameLayout>
</ScrollView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.