gpt4 book ai didi

android - 水平 ScrollView 内的自定义 View 不滚动

转载 作者:行者123 更新时间:2023-11-29 14:15:44 25 4
gpt4 key购买 nike

我正在处理自定义 View 。当我将自定义 View 放入水平 ScrollView 时,水平滚动不起作用。我想我在编写 MeasureSpec 时犯了错误,但我不知道如何为自定义 View

我的 View 类代码是这样的

public class Makecell extends ViewGroup{
private int line_height;
private int line_width;
Context mContext;
subview sub;
int left,right,top,bottom;
MainActivity main;
public Makecell(Context context) {
super(context);
mContext=context;
// TODO Auto-generated constructor stub
}

public Makecell(Context context, AttributeSet attrs){
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();

final int count = getChildCount();
int line_height = 0;
int line_width=0;

int xpos = getPaddingLeft();
int ypos = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {

child.measure(
MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));

final int childw = child.getMeasuredWidth();
final int childh = child.getMeasuredHeight();
line_height = Math.max(line_height, child.getMeasuredHeight());
line_width=Math.max(line_width, child.getMeasuredWidth());
if (xpos + childh > height) {
xpos = getPaddingLeft();
ypos += line_width;
}
xpos += childw;
}
}

this.line_width=line_width;


}

@SuppressLint("DrawAllocation")
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub

final int count = getChildCount();
final int width = r - l;
final int hight = b - t;
int xpos = 0;
int ypos = 0;

for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final int childw = child.getMeasuredWidth();
final int childh = child.getMeasuredHeight();
//
if (xpos + childh > hight) {
xpos = getPaddingLeft();
ypos += line_width;


}
child.layout(ypos, xpos,ypos + childw, xpos + childw );
xpos += childh;

}
}
}
}

xml 看起来像这样

<RelativeLayout 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"
tools:context=".MainActivity" >
<HorizontalScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true"
android:id="@+id/rootlin"
>
</HorizontalScrollView>
</RelativeLayout>

在代码中添加 View 看起来像

cell=new Makecell(getApplicationContext());
mainlin=(HorizontalScrollView)findViewById(R.id.rootlin);
LinearLayout lin=new LinearLayout(this);
lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
lin.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<100;i++)
{
text=new TextView(this);
text.setText("Text "+i);
text.setTextSize(15);
text.setBackgroundColor(Color.CYAN);
text.setWidth(screenwidth/2);
cell.addView(text);
}
lin.addView(cell);
mainlin.addView(lin);

谁能帮我找出我哪里做错了

提前致谢..

最佳答案

它们都有效:

在Java中:

    LinearLayout lin=new LinearLayout(this);
lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
lin.setOrientation(LinearLayout.HORIZONTAL);
lin.setHorizontalScrollBarEnabled(true);

在 xml 文件中:

   <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:scrollbars="horizontal"
android:orientation="horizontal"
tools:context=".MainActivity" >
</LinearLayout>

关于android - 水平 ScrollView 内的自定义 View 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378121/

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