gpt4 book ai didi

android - 滚动条到自定义 ViewGroup

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:24 27 4
gpt4 key购买 nike

我有 CustomComponent 类,它扩展了 ViewGroup

CustomComponent源代码:

      public class CustomComponent extends ViewGroup {

private static final String LOGTAG = "CustomComponent";

private List<MenuItem> items;

private Context context;

private int screenWidth;
private int screenHeight;
private int cellWidth;
private int cellHeight;
private int duration;
private int space=7;

public CustomComponent(Context context) {
super(context);
this.context=context;
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.v(LOGTAG, "on Measure called");
screenWidth = MeasureSpec.getSize(widthMeasureSpec);
screenHeight = MeasureSpec.getSize(heightMeasureSpec);
cellHeight=screenHeight/AppConstants.HEIDHTCELLSCOUNT;
cellWidth=screenWidth/AppConstants.WIDTHCELLSCOUNT;
duration= cellHeight*2;
super.onMeasure(widthMeasureSpec, heightMeasureSpec+duration);

}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.v(LOGTAG, "onLayout called");
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
child.layout(items.get(i).getLeft(),items.get(i).getTop(),items.get(i).getRight(), items.get(i).getBottom());
}
}

public List<MenuItem> getItems() {
return items;
}

public void setItems(List<MenuItem> items) {
this.items = items;
}
}

XML 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/menu_layout"
android:scrollbars="vertical">
<<package name>.CustomComponentandroid:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/menu_component"
android:scrollbars="vertical"
android:fadingEdge="vertical"/>
</LinearLayout>

我需要向这个 ViewGroup 添加垂直滚动。请帮忙,我不知道如何解决这个问题。 enter image description here

最佳答案

试试这个。您需要在 ScrollView 中使用 LinearLayout。将 MenuComponent 放在 LinearLayout 中。它应该有效。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/menu_layout"

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<<package name>.MenuComponent android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/menu_component"
android:scrollbars="vertical"
android:fadingEdge="vertical"/>
</LinearLayout>

</ScrollView>
</LinearLayout>

关于android - 滚动条到自定义 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9515461/

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