2014-05-21 48 views
0

這裏的一半地方的LinearLayout是我的應用程序:安卓:在高度

enter image description here

我想放置的LinearLayout(藍色)在屏幕的中間。 (事實上​​高度的一半)。

我不知道該怎麼做。

這裏是我的代碼:

<LinearLayout 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" 
    android:background="#E9E9E9" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" > 

    <TextView 
     android:id="@+id/txt_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="30dp" 
     android:text="title app" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="70sp" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="230dp" 
     android:background="@color/blue_dark_normal" 
     android:layout_centerVertical="true" 
     android:orientation="vertical" > 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="bottom" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:layout_marginLeft="15dp" 
      android:layout_marginRight="5dp" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/startact_btn_connect" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:background="@drawable/bg_blue_button_login" 
       android:text="Se connecter" 
       android:textColor="@color/blanc" /> 

      <View 
       android:layout_width="fill_parent" 
       android:layout_height="3dp" 
       android:layout_marginBottom="10dp" 
       android:background="@color/blue_pressed" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="15dp" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/startact_btn_view_annonces" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:background="@drawable/bg_blue_dark_button_login" 
       android:text="Voir les annonces" 
       android:textColor="@color/blanc" /> 

      <View 
       android:layout_width="fill_parent" 
       android:layout_height="3dp" 
       android:layout_marginBottom="10dp" 
       android:background="@color/blue_dark_pressed" /> 
     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

回答

1

使用RelativeLayout爲根,並且只需添加layout_centerInParent=trueLinearLayout要居中。

+0

這似乎不符合「一半高度」的要求,除非他知道確切的固定高度。 – Matt

+0

正如我理解這個問題,視圖必須是父代高度的一半,而不是父代高度的一半。 –

0

我相信你可以用重量做到這一點。

這裏是我的代碼,使中心的@半高度的LinearLayout,藍色:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
    android:id="@+id/topQuarterHeight" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" /> 

    <LinearLayout 
    android:background="#FF0000FF" 
    android:orientation="vertical" 
    android:id="@+id/midHalfHeight" 
    android:layout_weight="2" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"></LinearLayout> 

    <FrameLayout 
    android:id="@+id/bottomQuarterHeight" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" /> 

</LinearLayout> 

而這裏的預覽的樣子:

enter image description here

0

試試這個:

<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" 
android:background="#E9E9E9" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" > 

<TextView 
    android:id="@+id/txt_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="30dp" 
    android:text="title app" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textSize="70sp" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="230dp" 
    android:background="@color/blue_dark_normal" 
    android:layout_centerInParent="true" 
    android:orientation="vertical" > 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="5dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/startact_btn_connect" 
      style="?android:attr/borderlessButtonStyle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/bg_blue_button_login" 
      android:text="Se connecter" 
      android:textColor="@color/blanc" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="3dp" 
      android:layout_marginBottom="10dp" 
      android:background="@color/blue_pressed" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="15dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/startact_btn_view_annonces" 
      style="?android:attr/borderlessButtonStyle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/bg_blue_dark_button_login" 
      android:text="Voir les annonces" 
      android:textColor="@color/blanc" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="3dp" 
      android:layout_marginBottom="10dp" 
      android:background="@color/blue_dark_pressed" /> 
    </LinearLayout> 

</LinearLayout>