为自定义视图添加边距

时间:2017-08-01 12:26:20

标签: android view android-custom-view

我创建了一个自定义视图。 XML文件的内容 -

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/medium_margin"
    android:layout_marginTop="@dimen/medium_margin"
    android:background="@drawable/rectangle_rounded_border"
    android:padding="@dimen/medium_margin"
    tools:parentTag="RelativeLayout">

    <ImageView
        android:id="@+id/button_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button_text"
        android:layout_alignTop="@+id/button_text"
        android:layout_gravity="center"
        android:layout_marginEnd="@dimen/large_margin"
        android:layout_toStartOf="@+id/button_text"
        tools:src="@drawable/ic_star" />

    <TextView
        android:id="@+id/button_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:textColor="@color/envColor"
        android:textSize="@dimen/font_size_small"
        tools:text="My favorites" />

</merge>

及其专门的课程 -

public class ParameterButton extends RelativeLayout {

    private TextView title;
    private ImageView icon;

    private String text;
    private Drawable drawable;
    private Integer color;

    public ParameterButton(Context context) {
        super(context);
        inflateView(context);
    }

    public ParameterButton(Context context, String text, Integer drawable, Integer color) {
        super(context);
        this.text = text;
        if (drawable != null) {
            this.drawable = ResourcesUtils.getDrawable(drawable);
        }
        if (color != null) {
            this.color = ResourcesUtils.getColor(color);
        }
        inflateView(context);
    }

    public ParameterButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        retrieveAttributes(context, attrs);
        inflateView(context);
    }

    public ParameterButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        retrieveAttributes(context, attrs);
        inflateView(context);
    }

    public ParameterButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        retrieveAttributes(context, attrs);
        inflateView(context);
    }

    private void retrieveAttributes(Context context, AttributeSet attrs) {
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ParameterButton,
                0, 0);

        try {
            text = a.getString(R.styleable.ParameterButton_text);
            drawable = a.getDrawable(R.styleable.ParameterButton_iconDrawable);
            color = a.getColor(R.styleable.ParameterButton_iconTint, ContextCompat.getColor(context, android.R.color.black));
        } finally {
            a.recycle();
        }
    }

    private void inflateView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.button_parameter, this, true);
        title = (TextView) findViewById(R.id.button_text);
        icon = (ImageView) findViewById(R.id.button_icon);

        if (text != null) {
            title.setText(text);
            if (drawable != null) {
                icon.setImageDrawable(drawable);
                if (color != null) {
                    icon.setColorFilter(color);
                }
            }
        }

        setBackground(ResourcesUtils.getDrawable(R.drawable.rectangle_rounded_border));
        int pad = (int) getResources().getDimension(R.dimen.small_margin);
        setPadding(pad, pad, pad, pad);

        MarginLayoutParams margins = MarginLayoutParams.class.cast(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        int margin = (int) getResources().getDimension(R.dimen.medium_margin);
        margins.topMargin = margin;
        margins.bottomMargin = margin;
        setLayoutParams(margins);
    }
}

我无法为我的观点设置余量。如果我使用RelativeLayout代替merge,它的效果非常好。我需要以编程方式添加边距。

我最后添加的布局参数不考虑(但我的填充是)

我该怎么办?

0 个答案:

没有答案
相关问题