如何制作方形框架布局?
问题描述:
我试图通过扩展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);
这帮助我根据屏幕尺寸得到方框。
答
检出SquareLayout,Android库它为不同的布局提供了一个包装类,呈现它们的平方尺寸而不会丢失任何核心功能。
尺寸是在布局呈现之前计算的,因此一旦获得视图就没有重新渲染或任何可以调整的东西。
要使用库,添加到您的build.gradle:
repositories {
maven {
url "https://maven.google.com"
}
}
dependencies {
compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}
答
你可以看到从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);
}
答
我发现一个更“减少“解决方案。创建自己的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);
}
}
使用显示器指标,了解显示器的宽度和设置高度相同的代码宽度,这样它会出现方 – SaravInfern
ÿ 。谢谢。有效。 –
更新你的代码“陈述更新”,这对其他人会有帮助 – SaravInfern