Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 根据childs';调整水平滚动视图高度;高度_Android_Android Layout_Android Scrollview_Horizontalscrollview - Fatal编程技术网

Android 根据childs';调整水平滚动视图高度;高度

Android 根据childs';调整水平滚动视图高度;高度,android,android-layout,android-scrollview,horizontalscrollview,Android,Android Layout,Android Scrollview,Horizontalscrollview,我有一个水平滚动视图,其中我以编程方式添加了child。滚动视图非常简单: <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:id="@+id/child_container

我有一个
水平滚动视图
,其中我以编程方式添加了child。滚动视图非常简单:

<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>
问题是TextView中的文本可能较小(2行)或更大,最多可达7-8行。如何使此
水平滚动视图
根据文本调整其高度

在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

将线性布局高度设置为包裹内容。

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

<com.app.WrapContentHorizontalScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</com.app.WrapContentHorizontalScrollView>

希望这对别人有帮助。

你是说孩子们的线性布局?它不工作,我仍然只看到一行文本。您的问题是您的文本没有多行还是子高度和父高度错误?你的问题不清楚。尝试将xml中的最小行设置为1。我只是看不到第2-3-4-5行-。我的感觉是,在测量时,水平滚动视图只计算文本视图的1行。如果我用android:numLines硬编码值的数量,它将反映在最终高度中。但我希望高度基于文本长度,而不是预定义的值;如果我设置minLines=5,我会看到5行。但是还有更多。尝试删除xml中关于行的任何内容,看看让它自己处理是否可以。
<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);
    }

}