gpt4 book ai didi

android - 在部分内容上滚动 RelativeLayout- 白色边框

转载 作者:行者123 更新时间:2023-11-30 03:57:22 27 4
gpt4 key购买 nike

我有一个相当简单的 Fragment,它向 RelativeLayout 添加了一些彩色 ImageView。图片太多,屏幕放不下,所以我实现了一些自定义滚动。

但是,当我四处滚动时,我看到在我滚动之前屏幕边缘所在的位置有大约 90dp 的白色边框与内容重叠。很明显,ImageView 仍在正确创建和绘制,但它们被掩盖了。

我该如何摆脱它?

我试过:

  • 将 RelativeLayout 和 FrameLayout 都更改为 WRAP_CONTENT、FILL_PARENT、MATCH_PARENT 以及它们的一些组合。
  • 将两种布局的填充和边距设置为 0dp。

例子:

fragment :

public class MyFrag extends Fragment implements OnTouchListener {
int currentX;
int currentY;
RelativeLayout container;
final int[] colors = {Color.BLACK, Color.RED, Color.BLUE};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup fragContainer, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_myfrag, null);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

container = (RelativeLayout) getView().findViewById(R.id.container);
container.setOnTouchListener(this);

// Temp- Add a bunch of images to test scrolling
for(int i=0; i<1500; i+=100) {
for (int j=0; j<1500; j+=100) {
int color = colors[(i+j)%3];

ImageView image = new ImageView(getActivity());
image.setScaleType(ImageView.ScaleType.CENTER);
image.setBackgroundColor(color);

LayoutParams lp = new RelativeLayout.LayoutParams(100, 100);
lp.setMargins(i, j, 0, 0);
image.setLayoutParams(lp);

container.addView(image);
}
}
}

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
currentX = (int) event.getRawX();
currentY = (int) event.getRawY();
break;
}

case MotionEvent.ACTION_MOVE: {
int x2 = (int) event.getRawX();
int y2 = (int) event.getRawY();
container.scrollBy(currentX - x2 , currentY - y2);
currentX = x2;
currentY = y2;
break;
}
case MotionEvent.ACTION_UP: {
break;
}
}
return true;
}
}

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".FloorPlanFrag">

<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>

最佳答案

在查看 RelativeLayout source 时,我注意到 onMeasure() 调用了 applyHorizo​​ntalSizeRules(LayoutParams childParams, int myWidth)applyVerticalSizeRules(LayoutParams childParams, int myHeight)

applyHorizo​​ntalSizeRules 中,我发现对于 myWidth 和 myHeight 参数:

// -1 indicated a "soft requirement" in that direction. For example:        
// left=10, right=-1 means the view must start at 10, but can go as far as it wants to the right

myWidth 参数初始化为-1,然后根据MeasureSpec 的模式更改onMeasure() 的参数。

所以我创建了自己的扩展 RelativeLayout 的 View ,并覆盖 onMeasure() 以将模式设置为“未指定”:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);

int newWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED);
int newHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED);

super.onMeasure(newWidthSpec, newHeightSpec);
}

像魅力一样工作!

关于android - 在部分内容上滚动 RelativeLayout- 白色边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com