Android - match_parent 忽略 layout_margin

标签 android android-layout

我的 Activity 有一个带有单个 subview 的 LinearLayout。我希望两者都填满屏幕,减去 12 dp 的边距。

不幸的是, subview 被绘制了 12 dp 太大而被截断了。显然 match_parent 在计算 subview 的大小时忽略了 layout_margin 属性。解决此问题的最简单方法是什么?

myActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <com.myapp.myView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

myActivity.java

package com.myapp;

import android.app.Activity;
import android.os.Bundle;

public class myActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myActivity);
    }
}

myView.java

package com.myapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class myView extends View {

    private Paint paint = new Paint();

    public myView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(0xFFFF0000); //red
        paint.setStyle(Paint.Style.STROKE); // for unfilled rectangles
        paint.setStrokeWidth(4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int size = canvas.getWidth(); // width = height (see onMeasure())
        canvas.drawRect(0, 0, size, size, paint);   
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        // This gives us a square canvas!
    }
}

最佳答案

subview 周围可以有边距,父 View (或布局之类的 View 组)可以在它们的边界和 subview 之间有一个填充。换句话说,边距在 View 外,填充在 View 内。

另外,请看这个很好的解释:Difference between a View's Padding and Margin

使用标准 View 和填充而不是边距的示例:

我用标准 View 而不是您的自定义 View 创建了一个小示例,并按照上面的建议为 LinearLayout 使用填充,并且效果很好(见屏幕截图):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="12dp"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#ff0000"/>
</LinearLayout>

screenshot from layout editor

解决方案

事实证明,问题是您在自定义 View 的 onDraw 方法中使用了 canvas.getWidth()。使用 View 的 getWidth() 代替,解决了这个问题。最后:-)

关于Android - match_parent 忽略 layout_margin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14033788/

相关文章:

android - 如何将xml加载到webview android

android - getWindow() 方法无法使用

java - Android:Searchview 即使单击具有不同 id 的其他菜单项也会打开

android - 具有点击功能的自定义图像按钮

android-layout - 协调布局不适用于选项卡布局

android - Android中如何根据时间调暗屏幕

使用 Mockito 时不会模拟 Android 方法

java - 从 Android Intent 打开图库应用

android - 如何从android中的 ListView 按钮显示警报确认对话框

android - 可在两个方向上工作的交错网格布局管理器