我正在制作一个自定义视图,它应具有相同的高度和宽度。
我用以下方式绘制它:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = heightMeasureSpec;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
如果我只是在屏幕上添加一个视图,宽度/高度就可以了。但是,如果我将彼此相邻的两个视图相加,则应调整视图的大小(每个视图:屏幕的一半宽度)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<at.guger.widget.Light
android:id="@+id/lights_light1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp" />
<at.guger.widget.Light
android:id="@+id/lights_light2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp" />
</LinearLayout>
我该怎么做?
答案 0 :(得分:0)
让视图占用尽可能多的空间,但是将它们限制在父布局管理器中会不会更容易?您可以使用recyclerview实现它,并且您自己的LayoutManager重载了measureChild方法,以便它返回宽度屏幕除以适配器中的elememnt数量。不是一个有效的例子,而是一些事情:
// setting up your recycler view
mRecyclerView = (RecyclerView) mView.findViewById(R.id.recycler);
// attaching your custom adapter
mRecyclerAdapter = new MyRecyclerAdapter(getActivity(), this);
mRecyclerView.setAdapter(statusRecyclerAdapter);
// setting the layoutmanager
mRecyclerLayoutManager = new MyRecyclerLayoutManager(
getActivity(),
LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mRecyclerLayoutManager);
然后你将创建一个内部类MyRecyclerLayoutManager,如下所示:
private class MyRecyclerLayoutManager extends LinearLayoutManager {
MyRecyclerLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public void measureChild(View child, int widthUsed, int heightUsed) {
super.measureChild(child, widthUsed, heightUsed);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthSpec = View.MeasureSpec.makeMeasureSpec((int) Math.round(displaymetrics.widthPixels / mRecyclerAdapter.size()), View.MeasureSpec.EXACTLY);
final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),
getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
canScrollVertically());
child.measure(widthSpec, heightSpec);
}
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
super.measureChildWithMargins(child, widthUsed, heightUsed);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthSpec = View.MeasureSpec.makeMeasureSpec((int) Math.round(displaymetrics.widthPixels * 0.85), View.MeasureSpec.EXACTLY);
final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),
getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
canScrollVertically());
child.measure(widthSpec, heightSpec);
}
}
这里重要的位是widthSpec,它是窗口宽度除以recycleradapter大小。