Android 添加文本视图时线性布局展开

Android 添加文本视图时线性布局展开,android,android-layout,Android,Android Layout,我正在将TextView添加到LinearLayout中,但是当我用文本(TextView)填充它时,这个LinearLayout会扩展。不能将其更改为RelativeLayout或类似的内容,因为我遵循的是一本书中的指南。这是我的XML代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xm

我正在将
TextView
添加到
LinearLayout
中,但是当我用文本(
TextView
)填充它时,这个
LinearLayout
会扩展。不能将其更改为
RelativeLayout
或类似的内容,因为我遵循的是一本书中的指南。这是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="40"
        android:background="@android:color/holo_orange_light">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

                <TextView
                    android:text=""
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/textView" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="60">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@mipmap/ic_launcher"
            android:id="@+id/imageView"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>


我不确定您到底想让它做什么,但请尝试将TextView的布局宽度及其所在的线性布局更改为“匹配父项”。

我不确定您到底想让它做什么,但请尝试将TextView的布局宽度及其所在的线性布局更改为“匹配父项”.

当您将文本添加到文本视图时,该文本视图的宽度会随着文本变大而扩展,并且宽度设置为“wrap\u content”,因此其宽度是它所容纳文本的宽度。因此,作为该文本视图父视图的LinearLayout由于设置为“wrap_content”而获得更大的宽度。作为滚动视图变大的结果,由于外线布局变大

解决方法是将LinearLayout width设置为“match_parent”或一些任意宽度(“例如50dp”)。如果设置“匹配父项”,LinearLayout width将是屏幕的宽度,但您必须将“匹配父项”同时设置为LinearLayout和ScrollView


我希望当您将文本添加到文本视图时,这能回答您的问题。文本视图的宽度随着文本变大而扩大,并且宽度设置为“wrap_content”,所以它的宽度是它所容纳文本的宽度。因此,作为该文本视图父视图的LinearLayout由于设置为“wrap_content”而获得更大的宽度。作为滚动视图变大的结果,由于外线布局变大

解决方法是将LinearLayout width设置为“match_parent”或一些任意宽度(“例如50dp”)。如果设置“匹配父项”,LinearLayout width将是屏幕的宽度,但您必须将“匹配父项”同时设置为LinearLayout和ScrollView


我希望这能回答您的问题

您代码中的问题在xml文件中,您在该文件中指定了文本视图的高度:



代码中的问题出现在xml文件中,您在该文件中指定了TextView的高度:



当然,因为包含wrap\u内容的父视图将根据其子视图的宽度(在其子视图中最大)进行调整。您看到的是视图组的正常行为(如Linearlayout)。但你的问题是什么?您想要一种不同的行为吗?当然,因为具有wrap_内容的父对象将调整其子对象宽度(在其子对象中最大)。您看到的是视图组的正常行为(如Linearlayout)。但你的问题是什么?你想要一种不同的行为吗?
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="40"
    android:background="@android:color/holo_orange_light">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
      <!--Change your textView as below-->
            <TextView
                android:text=""
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"  
                android:id="@+id/textView" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="60">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageView"
        android:layout_weight="1" />
</LinearLayout>