Android线性布局高度超出屏幕高度

3

我有一个LinearLayout在我的主Activity中使用,它看起来如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context=".MainActivity" >

    <LinearLayout 
        android:id="@+id/activity_main_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

我正在内动态添加片段,但问题是,当我的片段高度大于屏幕高度时,底部的内容会被隐藏。我不能滚动或做任何操作。
我尝试将片段的布局包装在中,但不起作用,因为我的片段中有listview。我还尝试在每个容器上设置布局高度为3000dp,但也没有效果。
我正在动态添加的片段示例布局如下所示。我也正在动态添加内容到片段中。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment_listing_detail_linearLayout_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:orientation="vertical"
        android:paddingLeft="@dimen/fragment_padding_side"
        android:paddingRight="@dimen/fragment_padding_side" >

        <TextView
            android:id="@+id/fragment_listing_detail_textview_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Review Title"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RatingBar
            android:id="@+id/fragment_listing_detail_ratingbar_rating"
            style="@style/customRatingBarSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:stepSize="0.5"
            android:isIndicator="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Photos" />

        <View style="@style/divider" />         

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:paddingBottom="10dp" >

            <LinearLayout
                android:id="@+id/fragment_listing_detail_linearlayout_images"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

            </LinearLayout>
        </HorizontalScrollView>

        <TextView
            android:id="@+id/fragment_listing_detail_textview_review"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Reviews" />

        <View style="@style/divider" />         

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

            <ImageView
                android:layout_width="match_parent"
                android:paddingTop ="7dp"
                android:paddingBottom="7dp"
                android:layout_height="wrap_content"
                android:contentDescription="review divisor"
                android:src="@android:drawable/divider_horizontal_bright" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/fragment_listing_detail_reviews_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

       <Button
            android:id="@+id/fragment_listing_detail_button_add_review"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Review" />

       <ImageView
           android:id="@+id/fragment_listing_detail_imageview_testimage"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

    </LinearLayout> 

当我的碎片超出屏幕高度时,我该如何使其仍然可以滚动以查看底部视图?谢谢。


你尝试过设置LinearLayout属性android:layout_height="match_parent"吗? - yushulx
我已经尝试将片段的布局包装在ScrollView中,这是否意味着将您当前在ScrollView内部的根LinearLayout进行包装? - codeMagic
是的,我已经尝试过了。但是我的布局中有ListView,所以它们只会折叠而不是展开。ScrollView是唯一的选择吗? - c 2
在我看来,ScrollView 是你需要的。但是如果不知道布局实际上是什么样子,很难说是否可能有其他设计选项。 - codeMagic
我认为ScrollView是最好的选择。我已经决定用LinearLayout替换我的Fragment中的ListView。 - c 2
显示剩余2条评论
1个回答

6

我相信你希望ScrollView成为根View。试着用以下代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 tools:context=".MainActivity"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fillViewPort="true" >

<!-- ScrollViews can only have 1 child View, so the 1st LinearLayout below
     will be the "container" -->

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

<!-- This is where you want to put everything scrollable -->

</LinearLayout>
</ScrollView>

我希望这可以帮到你,愉快编码!

似乎ScrollView是正确的选择。可能不是在根目录下,而是在片段布局中。顺便提一下,你的xml中有语法错误(多了一个斜杠)。 - c 2
请注意,不建议将ListView放置在ScrollView内部。 - Vikram
抱歉,我刚刚打错了。感谢您指出。我个人从未遇到过将ScrollView作为根视图的问题,但每个人都有自己的偏好。@user2558882,你从哪里获取ListView - MattMatt
1
从问题来看:“我已经尝试将片段的布局包装在ScrollView中,但对我来说行不通,因为我的片段中有listview”。 - Vikram
抱歉,这就是我能想到的了。 - MattMatt
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接