根据孩子的身高调整水平滚动视图高度



我有一个HorizontalScrollView,我以编程方式添加孩子。滚动视图非常简单:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/child_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</HorizontalScrollView>

如前所述,在child_container中,我必须以编程方式添加子视图。子视图都是从这个布局膨胀的:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="@dimen/content_padding"
    android:orientation="vertical">
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp" />
    <TextView
        android:id="@+id/text"
        style="@style/TextAppearance.AppCompat.Small"
        android:singleLine="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

问题是TextView中的文本可以很小(2行)或更大,最多7-8行。我如何让这个HorizontalScrollView根据文本调整其高度?

在XML中,我可以将android:lines="10"设置为文本视图,但是实际的最大行数可能会改变,并且我不能硬编码该值。

我在运行时尝试了很多东西,比如请求布局和重新测量,但我可能做错了。谢谢你。

LinearLayout l = (LinearLayout) findViewById(R.id.child_container);
for (String text : texts) {
    View childView = inflater.inflate(...);
    TextView textView = childView.findViewById(R.id.text);
    textView.setText(text);
    l.addView(childView);
}
// Here do something on l 
// (or on its parent, the HorizontalScrollView)
// to change its height

设置你的线性布局高度为wrap_content

我最终创建了子类。这是我的外部布局:

<com.app.WrapContentHorizontalScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</com.app.WrapContentHorizontalScrollView>
这是我的子布局:
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp" />
    <TextView
        android:singleLine="false"
        android:layout_width="150dp"
        android:layout_weight="1"
        android:layout_height="0dp" />
</LinearLayout>

这是我的类:

public class WrapContentHorizontalScrollView extends HorizontalScrollView {
    private LinearLayout directChild;
    private int targetHeight;
    private final static int UNSPECIFIED = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    public WrapContentHorizontalScrollView(Context context) {
        this(context, null);
    }
    public WrapContentHorizontalScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public WrapContentHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        directChild = new LinearLayout(context, attrs, defStyleAttr);
        super.addView(directChild);
    }
    @Override
    public void addView(@NonNull View child) {
        if (directChild != null) {
            directChild.addView(child);
            child.measure(UNSPECIFIED, UNSPECIFIED);
            targetHeight = Math.max(targetHeight, child.getMeasuredHeight());
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int newHeight = MeasureSpec.makeMeasureSpec(targetHeight, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, newHeight);
    }
}

希望对大家有所帮助。

最新更新