【问题标题】:Android - nested layoutsAndroid - 嵌套布局
【发布时间】:2014-12-23 07:31:06
【问题描述】:

我已经构建了以下 XML 布局:

<?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="fill_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.3"
            android:gravity="center"
            android:src="@drawable/logo" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.7"
            android:gravity="center"
            android:text="@string/my_profile"
            android:textColor="#A669DA"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll2"
        android:background="#A669DA"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.15"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/TextView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="@string/payroll_header"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.65" >

        <LinearLayout
            android:id="@+id/ll3"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:weightSum="2" >

            <ExpandableListView
                android:id="@+id/expandableListView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fadeScrollbars="true" >
            </ExpandableListView>

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <ExpandableListView
                    android:id="@+id/expandableListView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </ExpandableListView>

            </HorizontalScrollView>

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

此 XML 布局的根元素是线性布局。它包含 2 个线性布局和一个滚动。由于滚动视图只能有一个子视图,因此它包含一个线性布局,该布局又包含一个可扩展的列表视图、水平滚动视图(其中包含一个可扩展的列表视图)和一个列表视图。如您所见,这是一个非常复杂的布局,我认为应该可以简化。基本上,我希望前 2 个线性布局始终占据屏幕的 35%,而滚动视图占据其余部分。这就是为什么我给第一个线性布局的权重为 0.2,给第二个线性布局的权重为 0.15,给 scrollView 的权重为 0.65。

在滚动视图中,我希望 3 个元素中的每一个都占用所需的空间,以便用户在没有看到所有内容时向下滚动。我知道 expandableListView 和 ListView 已经是可滚动的,所以我将在它们中禁用滚动,以便使用父级的滚动条。

但是,我在这个设计中遇到了几个问题:

1) 在第一个屏幕截图中,您可以看到一个expandableListView、horizo​​ntalScrollBar(带有一个expandableListView)和一个listView。 他们每个人都将高度设置为“包装内容”,所以我希望他们每个人都能根据需要占用尽可能多的空间。但是,您可以在第二个屏幕截图中看到,当我打开第二个可展开的 listView(水平滚动条内的那个)时,listview 不会向下移动以为展开的列表视图腾出空间。我怎样才能实现它,以便在上面的可扩展列表扩展时它们中的每一个都向下移动?唯一的方法是将它们全部组合到一个 expandableListView 中吗?

2) 我的第二个 expandableListView 在水平滚动条中,但是,我不能水平滚动它。我什至可以将水平滚动条放在垂直滚动条中吗?

【问题讨论】:

    标签: android xml android-layout android-xml


    【解决方案1】:

    首先,稍微简化一下:您的第二个 LinearLayout(0.15 版本)可以省略,因为它只有一个孩子。请务必调整该单个子项(TextView)的布局参数。

    对于您的问题 #1,请尝试在您的根视图上调用 invalidate() 或 requestLayout()。

    问题#2实际上已经解决了:Link

    我的总体印象是 ScrollViews 和 ListViews 的这种嵌套非常复杂。您是否考虑过 TabLayout 或 DrawerLayout 等替代方案?

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      相关资源
      最近更新 更多