我扩展了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);
}
}
答案 0 :(得分:3)
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
*
* @author RAW
*/
public class FlowLayout extends ViewGroup {
private final static int PAD_H = 2, PAD_V = 2; // Space between child views.
private int mHeight;
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
final int count = getChildCount();
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
int childHeightMeasureSpec;
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
else
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
mHeight = 0;
for(int i = 0; i < count; i++) {
final View child = getChildAt(i);
if(child.getVisibility() != GONE) {
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);
final int childw = child.getMeasuredWidth();
mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V);
if(xpos + childw > width) {
xpos = getPaddingLeft();
ypos += mHeight;
}
xpos += childw + PAD_H;
}
}
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
height = ypos + mHeight;
} else if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
if(ypos + mHeight < height) {
height = ypos + mHeight;
}
}
height += 5; // Fudge to avoid clipping bottom of last row.
setMeasuredDimension(width, height);
} // end onMeasure()
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = r - l;
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
for(int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if(child.getVisibility() != GONE) {
final int childw = child.getMeasuredWidth();
final int childh = child.getMeasuredHeight();
if(xpos + childw > width) {
xpos = getPaddingLeft();
ypos += mHeight;
}
child.layout(xpos, ypos, xpos + childw, ypos + childh);
xpos += childw + PAD_H;
}
}
} // end onLayout()
}
enter code here
******************************************************************************************
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.goumin.tuan.view.FlowLayout
android:id="@+id/goods_page_goods_level1_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.goumin.tuan.view.FlowLayout>
</LinearLayout>
</ScrollView>
答案 1 :(得分:0)
try to change you layout like below
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Linearlayout
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>
</Linearlayout>
</<ScrollView>