【问题标题】:Adjust HorizontalScrollView height based on childs' height根据孩子的身高调整水平滚动视图的高度
【发布时间】: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


    【解决方案1】:

    将您的线性布局高度设置为 wrap_content。

    【讨论】:

    • 你的意思是子 LinearLayout ?不行,我还是只能看到一行文字。
    • 您的问题是您的文本不是多行还是子高度和父高度错误?你的问题不清楚。尝试在 xml 中将 minLine 设置为 1。
    • 我只是看不到第 2-3-4-5-.. 行。我的感觉是,在测量时,Horizo​​ntalScrollView 仅计为 TextView 的 1 行。如果我用 android:numLines 硬编码值的数量,它会反映在最终高度中。但我希望高度基于文本长度,而不是预定义的值。
    • 如果我设置 minLines=3,我看到 3 行;如果我设置 minLines=5,我会看到 5 行。但还有更多..
    • 尝试删除 xml 中有关行的任何内容,看看是否让它自行处理。
    【解决方案2】:

    我最终进行了子类化。这是我的外部布局:

    <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);
        }
    
    }
    

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      相关资源
      最近更新 更多