添加文本视图时扩展的线性布局



我正在将TextView添加到LinearLayout中,但是当我用文本填充它时,这个LinearLayout会扩展(TextView)。将其更改为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 和它所在的 LinearLayout 的layout_width更改为"match_parent"。

当您向文本视图添加文本时,该文本视图的宽度会随着文本变大而扩展,并且宽度设置为"wrap_content",因此其宽度是它所包含的文本的宽度。因此,作为该文本视图的父视图的线性布局获得更大的宽度,因为它设置为"wrap_content"。结果,ScrollView变得更大,并且由于外部LinearLayout变得更大。

对此的解决方案是将LinearLayout宽度设置为"match_parent"或任意宽度(例如"50dp")。在设置"match_parent"的情况下,线性布局宽度将是屏幕宽度,但您必须将"match_parent"设置为线性布局和滚动视图。

我希望这能回答你的问题

代码中的问题在于 xml 文件,您在其中为 TextView 指定了高度:

<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>

最新更新