Подтвердить что ты не робот

Как создать собственный размер FloatingActionButton

FloatingActionButton по умолчанию имеет два размера (обычный и мини). Мне нужен еще один (больше обычного).

app:fabSize параметр из xml стал переменной private int mSize; в классе FloatingActionButton. Реальный размер будет определяться этой функцией:

final int getSizeDimension() {
    switch(this.mSize) {
    case 0:
    default:
        return this.getResources().getDimensionPixelSize(dimen.fab_size_normal);
    case 1:
        return this.getResources().getDimensionPixelSize(dimen.fab_size_mini);
    }
}

Поскольку он объявлен как final, я не могу его продлить. Есть идеи? Может быть, я могу переопределить ресурс dimen.fab_size_mini в моем приложении?

4b9b3361

Ответ 1

Поскольку я не использую FAB размера mini, я переопределял этот размер.

dimens.xml:

<resources>
    <dimen name="design_fab_size_mini">100dp</dimen>
</resources>

layout.xml(первый FAB будет больше второго):

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        app:fabSize="mini"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:src="@drawable/ic_photo_camera_white_24dp"

        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@id/addPhoto"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="0dp"
        android:src="@drawable/ic_photo_white_24dp"
        />

Ответ 2

Вы можете определить свой собственный FloatingActionButton таким образом

public class CustomSizeFloatingActionButton extends FloatingActionButton {

    public CustomSizeFloatingActionButton(Context context) {
        super(context);
    }

    public CustomSizeFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSizeFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        setMeasuredDimension((int) (width * 1.5f), (int) (height * 1.5f));
    }

}

вместо 1.5f вы можете определить заданный множитель.
Таким образом, вам не нужно переопределять ресурсы, которые могут повлиять на другие части приложения.