首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >添加TextView时LinearLayout扩展

添加TextView时LinearLayout扩展
EN

Stack Overflow用户
提问于 2017-01-17 10:11:11
回答 3查看 194关注 0票数 1

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

代码语言:javascript
运行
复制
<?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>
EN

回答 3

Stack Overflow用户

发布于 2017-01-17 10:17:31

我不确定您想要它做什么,但是尝试将TextView的layout_width和它所在的LinearLayout更改为"match_parent“。

票数 0
EN

Stack Overflow用户

发布于 2017-01-17 10:22:53

当你向TextView添加文本时,TextView的宽度随着文本变大而扩展,并且宽度被设置为"wrap_content“,所以它的宽度就是它所容纳的文本的宽度。结果,作为该TextView的父视图的LinearLayout变得更宽,因为它被设置为"wrap_content“。因此,ScrollView变得更大,也正因为如此,外部LinearLayout变得更大。

解决方案是将LinearLayout宽度设置为"match_parent“或某个任意宽度(例如”50dp“)。在设置"match_parent“的情况下,LinearLayout width将是屏幕的宽度,但您必须将"match_parent”设置为LinearLayouts和ScrollView。

我希望这回答了你的问题

票数 0
EN

Stack Overflow用户

发布于 2017-01-17 11:07:40

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

代码语言:javascript
运行
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41688025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档