【问题标题】:LinearLayout within Relative Layout XML相对布局 XML 中的 LinearLayout
【发布时间】:2014-01-27 09:47:38
【问题描述】:

我的应用程序的 LinearLayout 部分已完成。我现在正在使用RelativeLayout 在屏幕/活动的顶部添加一个小条,但我似乎将它与下图中我想要的方式相反。

棕褐色的线性布局在顶部的一个小栏中可见,这就是我希望白色的 RelativeLayout 和 LinearLayout 应该使用 RelativeLayout 占用的空白空间。我很困惑如何:

  • 更改 RelativeLayout,使其高度仅与顶部的栏一样高 (50dp)

  • LinearLayout 开始向下 50dp,使其顶部与 RelativeLayout 的底部相接

仅包含 LinearLayout 的原始 XML 文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#F8FBBF" >

                <ExpandableListView
                    android:id="@+id/lvExp"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:cacheColorHint="#00000000"/> 

    </LinearLayout>

RelativeLayout 包含 LinearLayout 的修改后的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#F8FBBF" >

                <ExpandableListView
                    android:id="@+id/lvExp"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:cacheColorHint="#00000000"/> 

    </LinearLayout>
</RelativeLayout>

感谢您提前提供的任何帮助。

【问题讨论】:

    标签: android android-linearlayout android-relativelayout


    【解决方案1】:

    你可以这样做:

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
        >
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="50dp" >
    
    </RelativeLayout>
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="#F8FBBF" >
    
                    <ExpandableListView
                        android:id="@+id/lvExp"
                        android:layout_height="match_parent"
                        android:layout_width="match_parent"
                        android:cacheColorHint="#00000000"/> 
    
        </LinearLayout>
    </LinearLayout>
    

    但这引入了嵌套布局。你可以通过:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="#F8FBBF"
            android:layout_margintop"50dip" >
    
                    <ExpandableListView
                        android:id="@+id/lvExp"
                        android:layout_height="match_parent"
                        android:layout_width="match_parent"
                        android:cacheColorHint="#00000000"/> 
    
        </LinearLayout>
    
    
    </RelativeLayout>
    

    只是在线性布局中添加一个margintop

    【讨论】:

    • 做到了!谢谢迷宫! (我只需要更改行,使其显示marginTop)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    相关资源
    最近更新 更多