我正在尝试创建自定义布局" SquareBox"基于相对布局将ImageView保持在中心。 此自定义布局将始终采用方形大小(取决于ImageView的高度和宽度)。
protected void onMeasure(int width, int height) {
super.onMeasure(width, height);
int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
int size;
if (measuredWidth > measuredHeight) {
size = measuredWidth;
} else {
size = measuredHeight;
}
setMeasuredDimension(size, size);
}
这是我的布局代码:
<com.example.tools.SquareBox
android:id="@+id/square_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/darker_gray">
<ImageView
android:id="@+id/canvas_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
tools:src="@drawable/demo_image" />
</com.example.tools.SquareBox>
工作正常,但图像视图不在此自定义视图的中心。 我试过以下: - &GT;机器人:layout_centerInParent =&#34;真&#34;对于ImageView。
- &GT;机器人:比重=&#34;中心&#34; for&#34; SquareBox&#34;。
- &gt;以编程方式将CENTER_IN_PARENT添加到SquareBox的onMeasure方法中的ImageView。
但没有任何效果。 我在这里缺少什么?
答案 0 :(得分:1)
将您的布局更改为此(将layout_width
更改为match_parent
):
<com.example.tools.SquareBox
android:id="@+id/square_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/darker_gray">
<ImageView
android:id="@+id/canvas_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
tools:src="@drawable/demo_image" />
</com.example.tools.SquareBox>
并将您的自定义布局更改为:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int size;
if(widthMode == MeasureSpec.EXACTLY && widthSize > 0){
size = widthSize;
}
else if(heightMode == MeasureSpec.EXACTLY && heightSize > 0){
size = heightSize;
}
else{
size = widthSize < heightSize ? widthSize : heightSize;
}
int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(finalMeasureSpec, finalMeasureSpec);
}
答案 1 :(得分:0)
试试这个: - android:layout_gravity="center_horizontal"
答案 2 :(得分:0)
我以另一种方式模仿了方框行为。我认为这是一种解决方法,它可能不是最好的解决方案,但它有效。我想知道更好更清洁的解决方案。
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/square_box_holder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="0dp"
android:layout_weight="3">
<View
android:id="@+id/square_box"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:background="@android:color/darker_gray" />
<ImageView
android:id="@+id/canvas_image"
style="@style/CanvasImageStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="0dp"
android:adjustViewBounds="true"
tools:src="@drawable/demo_image2" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
调整视图大小的代码:
squareImageView.setImageBitmap(getResultBitmap());
squareBoxHolder.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
squareBoxHolder.getViewTreeObserver().removeOnPreDrawListener(this);
Log.i(TAG, "onPreDraw: squareBoxHolder");
squareBoxHolderSize = new Point(squareBoxHolder.getMeasuredWidth(), squareBoxHolder.getMeasuredHeight());
return true;
}
});
squareImageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
squareImageView.getViewTreeObserver().removeOnPreDrawListener(this);
Log.i(TAG, "onPreDraw: squareImageView");
int imageWidth = squareImageView.getMeasuredWidth();
int imageHeight = squareImageView.getMeasuredHeight();
int size;
ViewGroup.LayoutParams layoutParams = squareImageView.getLayoutParams();
if (imageWidth > imageHeight) {
if (imageWidth > squareBoxHolderSize.y) {
size = squareBoxHolderSize.y;
layoutParams.width = size;
squareImageView.setLayoutParams(layoutParams);
} else {
size = imageWidth;
}
} else {
if (imageHeight > squareBoxHolderSize.x) {
size = squareBoxHolderSize.x;
layoutParams.height = size;
squareImageView.setLayoutParams(layoutParams);
} else {
size = imageHeight;
}
}
layoutParams = squareBox.getLayoutParams();
layoutParams.height = size;
layoutParams.width = size;
squareBox.setLayoutParams(layoutParams);
return true;
}
});