【问题标题】:How to force android layout to show two views如何强制android布局显示两个视图
【发布时间】:2013-04-24 04:44:50
【问题描述】:

我有想要组织的布局。这是它的代码:(部分)

<RelativeLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true" >

        <TableLayout
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#d9d9d9"
            android:clickable="true"
            android:stretchColumns="*" >
        </TableLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/scrollView1"
        android:background="@android:color/background_light"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/showingXoutOfY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/XOutOfY"
            android:textColor="@color/blue" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/copyright"
            android:textColor="@android:color/black" />
    </LinearLayout>
</RelativeLayout>

我希望ScrollView 填满屏幕,但我希望它为LinearLayout 留出空间,底部有TextViews。如您所见,我尝试将它们包装在RelativeLayout 中,并将ScrollView 对齐到顶部,将LinearLayout 对齐到底部,但它仍然只显示ScrollView。 嵌套在ScrollView 中的TableLayout 是动态填充的。

【问题讨论】:

  • 所以你想在LinearLayout里面的TextView上面显示scrollview。不是吗?
  • @shree202 是的。容器无所谓。如果我告诉我我想做什么,也许会更清楚。 TableLayout 是一个项目列表。 TextView 包含列表中有多少项目的信息
  • 尝试将滚动视图的高度设置为换行内容

标签: java android android-layout layout android-relativelayout


【解决方案1】:
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"

android:layout_above="@+id/LinearLayout1"

    android:layout_alignParentTop="true" >

    <TableLayout
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#d9d9d9"
        android:clickable="true"
        android:stretchColumns="*" >
    </TableLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:color/background_light"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/showingXoutOfY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/XOutOfY"
        android:textColor="@color/blue" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/copyright"
        android:textColor="@android:color/black" />
</LinearLayout>

检查以粗体显示的行,该参数表示它应该始终高于指定视图

【讨论】:

  • 做到了,eclipse 说:渲染过程中引发异常:RelativeLayout 中不能存在循环依赖
  • 我已经编辑了答案,这是由于线性布局说它应该在滚动视图下方,而滚动视图说它应该在线性布局上方。删除了@scrollview 下方的线性布局
  • 它成功了。我无法将答案标记为已接受,因为我不知道你们中的哪一个先回答,所以我将评论标记为有用...
【解决方案2】:

试试这个:

<RelativeLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
android:layout_above="@+id/LinearLayout1" >

        <TableLayout
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#d9d9d9"
            android:clickable="true"
            android:stretchColumns="*" >
        </TableLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/background_light"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/showingXoutOfY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/XOutOfY"
            android:textColor="@color/blue" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/copyright"
            android:textColor="@android:color/black" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 只需在滚动视图中添加 layout_above="@+id/LinearLayout1"
  • 做到了,eclipse 说:渲染过程中引发异常:RelativeLayout 中不能存在循环依赖
  • 这解决了您的问题吗?您也可以从 LinearLayout 中删除 layout_below 属性,因为它不需要。
  • 它成功了。我无法将答案标记为已接受,因为我不知道你们中的哪一个先回答,所以我将评论标记为有用...
  • 没问题的朋友。你可以接受任何答案,我不介意。很高兴能用我所知道的和从中学到的东西来帮助这里的人们。
【解决方案3】:

ScrollView 可以使用 marginBottom 属性。或者使用 LinearLayout 作为父级和布局权重属性来拉伸滚动视图

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多