【发布时间】:2015-10-23 15:23:36
【问题描述】:
我有一个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
【问题讨论】:
标签: android android-layout android-scrollview horizontalscrollview