简体   繁体   English

在线性布局中调整scrollview的高度属性

[英]adjusting height properties of scrollview within linear layout

I have a layout for an item in my listview. 我的列表视图中有一个项目的布局。 My problem is with the scroll view. 我的问题是滚动视图。 The comments textview which I am storing inside can vary in length anywhere from 2 words to 255 characters. 我存储在其中的注释textview的长度可以从2个单词到255个字符不等。 This creates a problem in my listview as when there is little text present it leaves a huge gap with the current setup. 这在我的列表视图中造成了一个问题,因为当文本很少时,它与当前设置之间存在很大的差距。 I don't want to get rid of the weight system as it is very helpful with all the other items in my layout,however I do not know how to use it here so that the height of the scrollview wraps around the content and doesn't take up more space then necessary. 我不想放弃权重系统,因为它对布局中的所有其他项目都非常有帮助,但是我不知道如何在这里使用它,以使滚动视图的高度环绕内容而不会不要占用更多的空间。 I've tried setting a fixed height for my scrollview as well as getting rid of the weighting for it, and using wrap content, however wrap content doesn't work when there is a large amount of text. 我尝试为滚动视图设置一个固定的高度,并消除了它的权重,并使用了换行内容,但是当有大量文本时,换行内容不起作用。 To summarise, when there is little text I'm left with a huge gap, when there is a lot of text, if I use wrap content it will fill up the whole screen and ruin the whole point of using a scrollview to save space. 总而言之,当文本很少时,我会留下很大的空白,当文本很多时,如果使用自动换行内容,它将填满整个屏幕,并破坏使用scrollview节省空间的整个过程。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="220dp"
android:orientation="vertical"
android:weightSum="250">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:orientation="horizontal"
    android:weightSum="100">


    <TextView
        android:layout_width="0dp"
    android:id="@+id/leaguetxt"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:gravity="center"
        android:text="English Premier League"
        android:textColor="#067103"
        android:textStyle="bold" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/datetxt"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:gravity="center"
        android:text="BET UNTIL : 23/05/2015 15:00 GMT"
        android:textColor="#067103"
        android:textSize="12sp"
        android:textStyle="italic" />


</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="35">

    <TextView
        android:id="@+id/teamstxt"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:text="Sevilla FC - FC Barcelona"
        android:textColor="#067103"
        android:textSize="23sp"
        android:textStyle="bold" />

</LinearLayout>



<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    android:orientation="horizontal">
    <!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->


    <!-- Name Label -->
    <TextView
        android:layout_width="0dp"
        android:id="@+id/bettxt"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="30"
        android:gravity="center"
        android:textSize="19sp"
        android:text="FC Barcelona Win and BTTS"
        android:textColor="#067103"
        android:textStyle="" />





</LinearLayout>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_weight="20"
    android:weightSum="100"
    android:orientation="horizontal"
    android:layout_height="0dp">

    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:text="DIFFICULTY : "
        android:layout_gravity="center"
        android:gravity="right"
        android:textColor="#067103"
        android:textStyle="italic"
        android:layout_weight="50"/>
    <TextView
        android:id="@+id/difficultytxt"
        android:layout_weight="50"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:gravity="left"
        android:textColor="#067103"
        android:text="MEDIUM RISK"
        android:layout_width="0dp"
        android:layout_height="fill_parent" />
</LinearLayout>
<ScrollView
    android:layout_width="fill_parent"
    android:layout_weight="90"
    android:layout_height="0dp">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:id="@+id/commenttxt"
        android:layout_height="fill_parent"
        android:textColor="#067103"
        android:text = ""/>
</LinearLayout>

</ScrollView>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_weight="50"
    android:weightSum="100"
    android:orientation="horizontal"
    android:layout_height="0dp">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="40"
        android:layout_height="fill_parent" />
    <Button
        android:id="@+id/oddsbutton"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="20"
        android:layout_gravity="center"
        android:gravity="center"
        android:onClick="SelectBet"
        android:text="3.60"
        android:textColor="#067103"
        android:textSize="18sp" />
</LinearLayout>

You shouldn't use a ScrollView inside a list item. 您不应在列表项内使用ScrollView。 It will cause glitches when scrolling the list because you would be scrolling both the list and the Scroll view of you item. 滚动列表时,这将导致故障,因为您将同时滚动列表和项目的“滚动”视图。 And it will also cause problem calculating the height of you scrollview such as the one you seem to be experiencing. 这也将导致计算滚动视图的高度(例如您似乎正在体验的滚动视图)的问题。

I think you should make you comment block to wrap_content and modify your layout to get rid of your layout_weight. 我认为您应该使您对wrap_content的注释块和修改布局以摆脱您的layout_weight。 If you don't want to always display long comments, you could elipsize them at a certain length and add a button to show/hide full comment. 如果不想总是显示长评论,则可以将其以一定长度省略,并添加一个按钮以显示/隐藏完整评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM