简体   繁体   English

LinearLayout android layout_height

[英]LinearLayout android layout_height

Help me with XML layout.帮助我进行 XML 布局。

I have three LinearLayout on activity, all LinearLayout have position vertical (from top to bottom)我在活动中有三个 LinearLayout,所有 LinearLayout 都有垂直位置(从上到下)

My LinearLayout positions我的 LinearLayout 位置

  1. first LinearLayout have android:layout_height="30px", android:layout_width="fill_parent"第一个 LinearLayout 有android:layout_height="30px", android:layout_width="fill_parent"
  2. second LinearLayout have android:layout_height="fill_parent", android:layout_width="fill_parent"第二个 LinearLayout 有android:layout_height="fill_parent", android:layout_width="fill_parent"
  3. third LinearLayout have android:layout_height="30px",android:layout_width="fill_parent"第三个 LinearLayout 有android:layout_height="30px",android:layout_width="fill_parent"

But when second LinearLayout set as fill_parent it fill full screen (from first Layout to bottom of screen), and third LinearLayout cant display!但是当第二个 LinearLayout 设置为fill_parent 时它会填充全屏(从第一个 Layout 到屏幕底部),而第三个 LinearLayout 无法显示!

How i need fill the second layout?我需要如何填充第二个布局?

Help me帮我

You can use relative layout for your purpose:您可以为您的目的使用相对布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout1"
    android:layout_above="@+id/linearLayout2"
    android:orientation="vertical" >
</LinearLayout>

</RelativeLayout>

Use this one simply.简单地使用这个。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#fbfbfb"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:orientation="vertical" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal" />
</LinearLayout>
</LinearLayout>

The trick is to not use fill_parent for this but 0dp and give it a weight with android:layout_weight="1" .诀窍是不要为此使用fill_parent而是使用0dp并使用android:layout_weight="1"给它一个权重。 This means that it will take all available extra space这意味着它将占用所有可用的额外空间

You have to give weight to middle linear layout so it can take full height.你必须给中间线性布局赋予权重,这样它才能占据全高。

try this for middle linear layout,

   <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
    </LinearLayout>

If you set height="fill_parent" to the middle layout, it will take the height of the parent view, so the 3rd view will not be visible (out of the screen)如果将 height="fill_parent" 设置为中间布局,它将占用父视图的高度,因此第三个视图将不可见(屏幕外)

1st layout : android:layout_height="30px", android:layout_width="match_parent"
2nd layout : android:layout_height="0dp", android:layout_width="match_parent", android:layout_weight="1"
3rd layout : android:layout_height="30px", android:layout_width="match_parent"

You should use "dp" instead of "px", to get the same size on different screen densities.您应该使用“dp”而不是“px”,以便在不同的屏幕密度下获得相同的尺寸。

("fill_parent" is deprecated, you should use "match_parent", it does the same) (不推荐使用“fill_parent”,您应该使用“match_parent”,它的作用相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM