我将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>发布于 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。
我希望这回答了你的问题
https://stackoverflow.com/questions/41688025
复制相似问题