如何制作方框布局?

时间:2016-11-22 11:06:36

标签: java android android-layout android-view android-framelayout

我尝试通过扩展android.widget.FrameLayout来制作自定义MySquareFrame类,并通过传递自定义宽度和高度覆盖onMeasure mehtod。

public class MySquareFrame extends FrameLayout {

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

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

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

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MySquareFrame(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int size = width > height ? height : width;
    setMeasuredDimension(size, size);
}
}

并在xml中使用此

<com.example.akash.view.MySquareFrame
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background3">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/fuel_meter"
    android:rotation="120"
    />
<com.triggertrap.seekarc.SeekArc
    android:id="@+id/seekArc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    seekarc:max="120"
    android:padding="70dp"
    seekarc:rotation="180"
    seekarc:startAngle="30"
    seekarc:sweepAngle="300"
    seekarc:touchInside="false"
    seekarc:clockwise="false"
    seekarc:thumb="@drawable/nob" />
</com.example.akash.view.MySquareFrame>

我得到了什么 Landscape view

所以我希望MySquareFrame类在xml中看起来像Square。请帮忙..

--------------------修订------------------------- < / p>

DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
    int size = width > height ? height : width;
    setMeasuredDimension(size, size);

这有助于我根据屏幕尺寸获得方框。

4 个答案:

答案 0 :(得分:2)

您可以在Android开发者网站上看到SquareFrameLayout的实现:

    

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSize == 0 && heightSize == 0) {
            // If there are no constraints on size, let FrameLayout measure
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

            // Now use the smallest of the measured dimensions for both dimensions
            final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
            setMeasuredDimension(minSize, minSize);
            return;
        }

        final int size;
        if (widthSize == 0 || heightSize == 0) {
            // If one of the dimensions has no restriction on size, set both dimensions to be the
            // on that does
            size = Math.max(widthSize, heightSize);
        } else {
            // Both dimensions have restrictions on size, set both dimensions to be the
            // smallest of the two
            size = Math.min(widthSize, heightSize);
        }

        final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        super.onMeasure(newMeasureSpec, newMeasureSpec);
    }

答案 1 :(得分:0)

查看SquareLayout,一个 Android库,它为不同的布局提供了一个包装类,使它们在不丢失任何核心功能的情况下进行尺寸标注。

尺寸是在渲染布局之前计算的,因此在获得视图后无需重新渲染或进行任何调整。

要使用库,请将其添加到build.gradle:

repositories {
    maven {
        url "https://maven.google.com"
    }
}

dependencies {
    compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}

答案 2 :(得分:0)

我为此找到了一个“减少”的解决方案。按如下所示创建自己的OneOneFrameLayout,然后将此FrameLayout添加到xml中:

public class OneOneFrameLayout extends FrameLayout {

public OneOneFrameLayout(@NonNull Context context) {
    super(context);
}

public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public OneOneFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int snHeight = MeasureSpec.getSize(widthMeasureSpec);
    int snHeightSpec = MeasureSpec.makeMeasureSpec(snHeight, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, snHeightSpec);
}
}

答案 3 :(得分:0)

出于想要使用GridLayoutManager进行方形查看的目的,Koopa给出了很好的答案,但是您已经有了widthMeasureSpec。 宽度很重要,然后获取高度以匹配宽度:

package com.my.package;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class SquareFrameLayout extends FrameLayout {

    public SquareFrameLayout(@NonNull Context context) {
        super(context);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec); //<-- Width + Width...
    }
}

只需在xml布局文件中使用它即可

<?xml version="1.0" encoding="utf-8"?>
<com.my.package.SquareFrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

    //<!--Views..-->

</com.my.package.SquareFrameLayout>