0

我試圖通過擴展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。請幫忙..

-------------------- UPDATED --------------------- ----

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

這幫助我根據屏幕尺寸得到方框。

+0

使用顯示器指標,瞭解顯示器的寬度和設置高度相同的代碼寬度,這樣它會出現方 – SaravInfern

+0

ÿ 。謝謝。有效。 –

+0

更新你的代碼「陳述更新」,這對其他人會有幫助 – SaravInfern

回答

0

檢出SquareLayout,Android庫它爲不同的佈局提供了一個包裝類,呈現它們的平方尺寸而不會丟失任何核心功能。

尺寸是在佈局呈現之前計算的,因此一旦獲得視圖就沒有重新渲染或任何可以調整的東西。

要使用庫,添加到您的build.gradle:

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

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

你可以看到從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); 
    } 
 
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); 
} 
}