Mat*_*att 22 android margin android-linearlayout
组布局中的边距似乎不起作用.
例如,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="40dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a button" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
应显示一个四边有40p边距的按钮.但是,它在右侧和底部有80p的边距.
难道我做错了什么?这是一个错误吗?
解决方法是使用重力,但这只适用于均匀的边距.
顺便说一句,这里有一个类似的问题,但尚未得到解答.
Rob*_*ond 23
android:padding="40dp"在LinearLayout或android:layout_margin="40dp"Button上将为您提供所需的效果.填充定义视图边缘与其内容之间的空间,布局边距定义视图两侧的额外空间.
big*_*nes 12
问题实际上是FrameLayout解释利润的方式.setContentView()将"主"布局附加到a FrameLayout,它是视图层次结构的实际根(您可以通过层次结构查看器查看)并通过电话提供给您.
边距由父布局管理,因此在本例中为main FrameLayout.我不知道它是一个功能还是一个bug,但这就是这种布局如何解释边距.
好吧,我在输入时已经发布了解决方案:使用填充代替.
如果您需要为布局设置边距,只需使用其他线性或相对布局进行包装即可
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_margin="40dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a button" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42405 次 |
| 最近记录: |