2017-02-17 151 views
1

您好,我必须使用常规Android布局创建UI,如下图所示(附图)。我得到了页眉,页脚和中间区域以及图像视图。 根据画面:区域A和区域C是类似的高度(屏幕的20%)和图像视图应该总是在B区和C区的中间 enter image description hereAndroid布局在另外两个布局的中间放置一个布局

我的当前xml代码是这样被放置

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    > 
 

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

 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:id="@+id/show_contact_middleArea" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="150dp" 
 
     android:orientation="vertical" 
 
     android:background="@color/grayContact" 
 
     android:layout_below="@+id/show_contact_headerArea" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_alignParentStart="true" 
 
     android:layout_above="@+id/show_contact_bottomArea"> 
 
     
 
    </LinearLayout> 
 

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

 
    <ImageView 
 
     android:layout_width="150dp" 
 
     android:layout_height="150dp" 
 
     app:srcCompat="@drawable/common_google_signin_btn_icon_dark_focused" 
 
     android:id="@+id/imageView2" 
 
     android:layout_marginBottom="40dp" 
 
     android:layout_alignParentBottom="true" 
 
     android:layout_centerHorizontal="true" /> 
 

 
</RelativeLayout>

我给

marginBot tom =“40dp”

图像视图向上推,这不是一个好的做法。将图像视图放置在区域B和区域C边界的中心的最佳方式是什么?

也是目前我给

机器人:layout_height = “120dp”

为A区& B区的高度,但最好都应该是在屏幕上,怎样的20%(每个)实现呢?

+0

使用的FrameLayout。它对这些种类或要求灵活 – Stallion

回答

2

你可以给权重20%,至每一个ç(你希望出现在B的部分高度)。这样他们将分别占据顶部和底部的20%。将剩余高度的60%分配给B

对于imageView,您可以将整个布局放置在坐标布局中,并将锚点分配给页脚。

她是代码片段

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


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

     <LinearLayout 

      android:id="@+id/show_contact_bottomArea" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_weight="2" 
      android:background="@color/primary_light" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/show_contact_middleArea" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_above="@+id/show_contact_bottomArea" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/show_contact_headerArea" 
      android:layout_weight="6" 
      android:background="@color/holo_blue_light" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/show_contact_headerArea" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignParentTop="true" 
      android:layout_weight="2" 
      android:background="@color/primary" 
      android:orientation="vertical"></LinearLayout> 


    </LinearLayout> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="40dp" 
     app:layout_anchor="@+id/show_contact_headerArea" 
     app:layout_anchorGravity="center_horizontal|top" 
     app:srcCompat="@drawable/header_record" /> 


</android.support.design.widget.CoordinatorLayout> 

结果: - 我有彩色的不同部分具有不同的颜色代码。

enter image description here

+0

谢谢你的答案,它的工作原理。是否可以根据区域C的高度设置图像视图的高度?我希望imageview的高度为C区高度的一半? –

+0

找到C线性布局的高度http://stackoverflow.com/questions/12068945/get-layout-height-and-width-at-run-time-android,然后动态设置它的高度http://stackoverflow.com/问题/ 3144940 /设置imageview的宽度和高度,以编程 –

0

使用重量为给予和区的确切部分帧布局 为例如: 重量总和= 4具有在母体线性布局,得到 区域A得到1- 区域B得到2- 区域C得到1-

B区后添加图像查看与顶部 -