使用layout_gravity ="bottom"放置在LinearLayout的底部

Sha*_*zon 10 android android-layout

我想在LinearLayout的底部放置一个布局,但我似乎无法让它工作.我知道我可以使用RelativeLayout来做到这一点,但我应该可以使用LinearLayout,不应该吗?

编辑:实际上这比我想象的更令人困惑.下面的布局是简化的.实际上,我正在使用兼容层在3.0之前的设备上使用片段.

我使用Hierarchy Viewer来检查发生了什么,并发现android.support.v4.app.NoSaveStateFrameLayout已添加到我的布局中,并且该布局将layout_height设置为wrap_content.这似乎是造成我的问题的原因,但我还没弄清楚如何解决它.

具体来说,为什么这不起作用?layout_gravity不应该把它放在底部吗?

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

        ... stuff here ...

        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            ... more stuff here ...

     </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,将layout_height更改为fill_parent或设置layout_weight似乎也不起作用.我只是想更好地了解发生了什么,因为很明显我错过了一些重要的东西.谢谢.

Rav*_*att 17

首先是好问题.

在这种情况下,Android表现得很奇怪.

如果您已选择父线性布局的水平方向,则可以通过设置其layoug_gravity = bottom将其子组件设置在底部.假设您在该水平线性布局中添加了2个文本视图,并且第二个textview的layout_gravity是底部,那么它将设置为bottom但它的工作方式就像它设置在其他列的底部然后是第一个文本视图.注意:当其parent linearlayout为水平但您无法看到其结果时,可以设置textview的layout_gravity ="left"或"right".

相反,如果您选择了父线性布局的垂直方向,则可以使用layout_gravity在左侧或右侧设置其子组件.但是第二个文本视图将显示在你可以说你设置的左右重力的下一行.注意当您的线性布局是垂直的但您无法看到其结果时,您可以设置textview的layout_gravity ="top"或"bottom".

尝试按照上面的说明制作样本xml设计,这样你就会有更好的想法.

奇怪但真实!尝试了解这种行为.:)


Sha*_*zon 10

所以我解决了这个问题.这是一个由两部分组成的解决方案:

首先,在不使用LinearLayout的情况下执行此操作的方法是为上面的元素提供权重,以便占用所有空白空间.顺便说一下,你可以在API演示中看到这个例子:http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_3.html

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

        ... stuff here ...
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weight="1"/>
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            ... more stuff here ...

     </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这本身并没有解决我的问题,因为我有一个带有layout_width ="wrap_content"的NoSaveStateFrameLayout作为父视图,所以我需要先修复它.我正在使用基于精彩Google I/O应用程序的代码,当我搜索NoSaveStateFrameLayout的代码时,我发现了这个:

// For some reason, if we omit this, NoSaveStateFrameLayout thinks we are
// FILL_PARENT / WRAP_CONTENT, making the progress bar stick to the top of the activity.
mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
        ViewGroup.LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)

感谢Google的一个很棒的评论!我把它添加到我的源代码中,一切都很棒!

故事的寓意:层次结构查看器和评论是你的朋友.

  • 我认为应该是"layout_weight"而不仅仅是"权重" - 至少这似乎有效,以及[sdk_dir] /samples/android-8/ApiDemos/res/layout/linear_layout_3.xml文件中的内容 (2认同)

小智 7

只需在底部和剩下的部分之间添加空格:

        <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)