Android 安卓:将线性布局放在一半高度上

Android 安卓:将线性布局放在一半高度上,android,android-layout,Android,Android Layout,这是我的应用程序: 我想在屏幕中间放置一个线性布局(蓝色)。(实际上是身高的一半) 我不知道怎么做 这是我的密码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heigh

这是我的应用程序:

我想在屏幕中间放置一个线性布局(蓝色)。(实际上是身高的一半)

我不知道怎么做

这是我的密码:

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


使用
RelativeLayout
作为根,只需将
layout\u centerInParent=true
添加到要居中的
线性布局中。

我相信可以通过权重实现这一点

以下是我的代码,以蓝色创建以半高为中心的线性布局:

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

下面是预览的样子:

试试这个:

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


除非他知道确切的固定高度,否则这似乎不符合“一半高度”的要求。正如我所理解的问题,视图必须是父视图高度的一半,而不是父视图高度的一半。