使用layout_gravity ="底部"放置在的LinearLayout的底部 [英] Using layout_gravity="bottom" to place at bottom of LinearLayout

查看:160
本文介绍了使用layout_gravity ="底部"放置在的LinearLayout的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向布局上的LinearLayout的底部,但我似乎无法得到它的工作。我知道,我可以使用RelativeLayout的做到这一点,但我应该能够使用的LinearLayout,应该不是我?

编辑:其实这比我想象的更混乱。下面的布局被简化。在现实中,我使用的是pre-3.0设备上的片段,与兼容层。

我用层次查看器查看发生了什么事情,发现一个android.support.v4.app.NoSaveStateFrameLayout添加到我的布局,而布局有layout_height设置为WRAP_CONTENT。这似乎是什么导致我的问题,但我还没有想出如何解决它。

具体来说,为什么不工作的呢?底部应该不是layout_gravity到位了吗?

 <的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>        ...东西在这里...        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =底
            机器人:方向=横向>            ...这里更多的东西...     < / LinearLayout中>
< / LinearLayout中>

BTW,改变layout_height到FILL_PARENT或设置似乎layout_weight不要么工作。我只是想更好地了解正在发生的事情,因为显然我失去了一些重要的东西。谢谢你。


解决方案

所以,我解决了这个问题。这是一个两部分的解决方案:

首先,要做到这一点,而无需使用的LinearLayout的方式是提供重​​量的元件的上方,使得它占用的所有空的空间的。顺便说一句,你可以看到在API演示这个例子:<一href=\"http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_3.html\">http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_3.html

 &LT;的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;        ...东西在这里...
        &LT;的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:体重=1/&GT;
        &LT;的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向&GT;            ...这里更多的东西...     &LT; / LinearLayout中&GT;
&LT; / LinearLayout中&GT;

这本身并没有解决我的问题,因为我有一个NoSaveStateFrameLayout与layout_width =WRAP_CONTENT作为父视图,所以我需要去首先解决。我使用code的基础上的精彩谷歌I / O应用,当我搜索在code代表NoSaveStateFrameLayout,我发现这一点:

  //出于某种原因,如果我们忽略这一点,NoSaveStateFrameLayout认为我们是
// FILL_PARENT / WRAP_CONTENT,使得进度条粘到活动的顶部。
mRootView.setLayoutParams(新ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
        ViewGroup.LayoutParams.FILL_PARENT));

感谢一个真棒评论谷歌!我说这个到我的源和一切工作太棒了!

这个故事的寓意是:层次浏览器和评论是你的朋友<。 / p>

I would like to place a layout on the bottom of a LinearLayout, but I can't seem to get it to work. I know that I can use RelativeLayout to do this, but I should be able to use LinearLayout, shouldn't I?

EDIT: Actually this is more confusing than I thought. The layout below is simplified. In reality, I'm using fragments on a pre-3.0 device, with the compatibility layer.

I used Hierarchy Viewer to examine what's going on, and found that an android.support.v4.app.NoSaveStateFrameLayout was added to my layout, and that layout has layout_height set to wrap_content. That seems to be what's causing my problem, but I haven't yet figured out how to fix it.

Specifically, why doesn't this work? Shouldn't the layout_gravity place it at the bottom?

<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>

BTW, changing layout_height to fill_parent or setting layout_weight don't seem to work either. I just want to better understand what is going on, because clearly I'm missing something important. Thanks.

解决方案

So I resolved the problem. It's a two-part solution:

First, the way to do this without using LinearLayout is to provide weight to the element above so that it takes up all of the empty space. BTW, you can see this example in the API demos: 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>

This by itself didn't solve my problem, as I had a NoSaveStateFrameLayout with layout_width="wrap_content" as a parent view, and so I needed to get that fixed first. I'm using code based on the wonderful Google I/O App, and when I searched the code for NoSaveStateFrameLayout, I found this:

// 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));

Thanks for an awesome comment Google!!! I added this into my source and everything worked great!

The moral of the story: Hierarchy Viewer and comments are your friends.

这篇关于使用layout_gravity =&QUOT;底部&QUOT;放置在的LinearLayout的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆