我制作了一个我需要在我的应用程序中使用的自定义Viewgroup
,但我需要将其放在ScrollView
中。只使用我的自定义ViewGroup
进行布局时,一切正常,但当我将其放入ScrollView
时,我看不到任何内容。
我的布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.test.CustomLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.test.CustomLayout>
</ScrollView>
我的观点组:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/* do something and call for each child
View v = getChildAt(i);
v.measure(wspec, hspec);
*/
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
//do something and call layout on every child
}
更新: 我的CustomLayout类
public class CustomLayout extends ViewGroup{
/*My params*/
public CustomLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
//do something and call layout on every child
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/* do something and call for each child
View v = getChildAt(i);
v.measure(wspec, hspec);
*/
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
}
更新2: 抱歉,但我做了一些其他的尝试,如果我在onMeasure方法的滚动视图中有viewgroup,我得到heightMeasureSpec = 0,然后如果我把viewgroup放在任何其他布局,我得到一个整数。也许这会有所帮助?
答案 0 :(得分:6)
我明白了,我必须自己测量Viewgroup,否则我根本没有高度 所以:
int heightMeasured = 0;
/*for each child get height and
heightMeasured += childHeight;*/
//If I am in a scrollview i got heightmeasurespec == 0, so
if(heightMeasureSpec == 0){
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasured, MeasureSpec.AT_MOST);
}
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
现在对我有用。
答案 1 :(得分:0)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
尝试将android:layout_height更改为match_parent
答案 2 :(得分:0)
你也在下面使用这个构造函数,对吗?因为这是在xml中创建视图时调用的。
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// Do your stuff
}
答案 3 :(得分:0)
您正在使用wrap_content
作为身高:
<com.example.test.CustomLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
由于没有内容,哪个会解析为0px afaik - 尝试将组件设为默认的最小高度
答案 4 :(得分:0)
检查这将完美无缺!
public class MaxHeightScrollView extends ScrollView {
public static int DEFAULT_MAX_HEIGHT = -1;
private int maxHeight = DEFAULT_MAX_HEIGHT;
public MaxHeightScrollView(Context context) {
super(context);
}
public MaxHeightScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ScrollMaxHeight,
0, 0);
try {
setMaxHeight(a.getInt(R.styleable.ScrollMaxHeight_maxHeight, 0));
} finally {
a.recycle();
}
}
public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (maxHeight != DEFAULT_MAX_HEIGHT
&& heightSize > maxHeight) {
heightSize = maxHeight;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
getLayoutParams().height = heightSize;
} else {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec)
, getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
}
在值文件夹中生成attr.xml
并添加以下代码
<resources>
<declare-styleable name="ScrollMaxHeight">
<attr name="maxHeight" format="integer"/>
</declare-styleable>
</resources>
像这样使用
<com.yourapppackage.customviews.MaxHeightScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:maxHeight="200"
android:fadeScrollbars="false"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...........
............
</LinearLayout>
</com.yourapppackage.customviews.MaxHeightScrollView>