【问题标题】:How to make a Square Frame Layout?如何制作方形框架布局?
【发布时间】:2016-11-22 11:06:36
【问题描述】:

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

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>

以及我得到的

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

--------已更新--------------

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);

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

【问题讨论】:

  • 使用显示度量来获取显示的宽度,并在代码中设置高度与宽度相同,使其显示为正方形
  • 谢谢。它奏效了。
  • 更新你的代码“声明updated”这将对其他人有所帮助

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


【解决方案1】:

你可以从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); }

【讨论】:

【解决方案2】:

查看SquareLayout,这是一个Android 库,它为不同的布局提供了一个包装类,在不丢失任何核心功能的情况下将它们呈现为平方尺寸。

尺寸是在布局渲染之前计算的,因此一旦获得视图,就没有重新渲染或任何类似的调整。

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

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

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

【讨论】:

    【解决方案3】:

    我为此找到了一个大大“简化”的解决方案。如下创建您自己的 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);
    }
    }
    

    【讨论】:

      【解决方案4】:

      为了使用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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 2018-02-22
        • 1970-01-01
        • 2021-08-10
        • 2010-11-07
        • 1970-01-01
        相关资源
        最近更新 更多