我已经扩展了 ViewGroup。我将自定义 ViewGroup 放入 ScrollView 中,动态添加一堆内容并且它不会滚动。流出屏幕的内容是不可见的。在下面的 xml 文件中,如果我删除了 Scrollview:
<ScrollView
android:id="@+id/myalltags_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
它可以工作。
我错过了什么?如何让我的 ViewGroup 与 ScrollView 兼容?
package com.android.picker;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyViewGroup extends ViewGroup {
private final static String TAG = "MyViewGroup";
public final static int VIEW_MARGIN=2;
private final static int MAX_TAG_LINES=3;
private Button lastMoreButton;
public int totalHeight=0;
public MyViewGroup(Context context) {
super(context);
Log.i(TAG,"myViewgroup");
}
public void fullTags(Context context,List<String> rs,Button button){
for(String s:rs){
Button v=new Button(context);
v.setText(s);
this.addView(v);
};
if(button!=null){
this.addView(button);
this.lastMoreButton=button;
}
}
public void fullTags(Context context,List<String> rs){
this.fullTags(context,rs,null);
}
public MyViewGroup(Context context,AttributeSet attributeSet){
super(context,attributeSet);
Log.i(TAG,"myViewgroupattributeSet");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec);
for (int index = 0; index < getChildCount(); index++) {
final View child = getChildAt(index);
// measure
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
totalHeight+=child.getMeasuredHeight();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {
Log.d(TAG, "changed = "+arg0+" left = "+l+" top = "+t+" right = "+r+" botom = "+b);
final int count = getChildCount();
int row=0;// which row lay you view relative to parent
int lengthX=l; // right position of child relative to parent
int lengthY=t; // bottom position of child relative to parent
for(int i=0;i<count;i++){
final View child = this.getChildAt(i);
Log.i("aa",((Button)child).getText().toString());
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
lengthX+=width+VIEW_MARGIN;
//if it can't drawing on a same line , skip to next line
if(lengthX>r){
lengthX=width+VIEW_MARGIN+l;
row++;
}
lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+t;
child.layout(lengthX-width, lengthY-height, lengthX, lengthY);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/myalltags_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<com.android.picker.MyViewGroup android:id="@+id/myalltags"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.android.picker.MyViewGroup>
</ScrollView>
</LinearLayout>
package com.android.picker;
import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
public class MyTagsDialog extends Dialog{
private Context context;
private MyViewGroup myAllTagsView;
public MyTagsDialog(Context context) {
super(context);
this.context=context;
this.setContentView(R.layout.mytagslayout);
initView();
initData();
}
private void initData() {
List<String> rs=new ArrayList<String>();
rs.add("test1");
rs.add("test2");
rs.add("test3");
rs.add("test4");
rs.add("test5");
rs.add("test6");
rs.add("test7");
rs.add("test8");
rs.add("test9");
rs.add("test10");
rs.add("test11");
rs.add("test12");
myAllTagsView.fullTags(context,rs);
// MyViewGroupPackage mypackages=new MyViewGroupPackage();
// mypackages.hasmore=true;
// mypackages.imems=rs;
// myAllTagsView.fullTags(context, rs);
// ViewGroup.LayoutParams params = myAllTagsView.getLayoutParams();
// params.height = myAllTagsView.totalHeight + 100;
// Log.i("cccc",""+params.height);
// myAllTagsView.setLayoutParams(params);
}
private void initView() {
myAllTagsView=(MyViewGroup)this.findViewById(R.id.myalltags);
}
}