我試圖通過擴展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>
所以我想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);
這幫助我根據屏幕尺寸得到方框。
使用顯示器指標,瞭解顯示器的寬度和設置高度相同的代碼寬度,這樣它會出現方 – SaravInfern
ÿ 。謝謝。有效。 –
更新你的代碼「陳述更新」,這對其他人會有幫助 – SaravInfern