Android 布局将一个布局放在另外两个布局的中间
Android layout place a layout in the middle of another two layouts
你好,我必须使用常规 Android 布局创建如下所示的 UI(附图片)。我得到了页眉、页脚和中间区域以及图像视图。
根据图片:区域 A 和区域 C 的高度相似(屏幕的 20%),图像视图应始终放置在区域 B 和区域 C 的中间。
我现在的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>
我给了
marginBottom="40dp"
图像视图将其向上推,这不是一个好的做法。将图像视图放置在区域 B 和区域 C 边界中心的最佳方式是什么。
目前我还给了
android:layout_height="120dp"
对于区域 A 和区域 B 的高度,但理想情况下两者都应为屏幕的 20%(每个),如何实现?
使用权重来给出区域和框架布局的确切部分
例如:
weight sum =4 必须在parent Linear layout中给出
A区送1
b区给2
c区给1
在B区后添加图片View with top -(你希望出现在B区的高度)
您可以为每个 A 和 C 赋予 20% 的权重。这样他们将分别占据顶部和底部的 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>
结果 :- 我用不同的颜色代码为不同的部分着色。
你好,我必须使用常规 Android 布局创建如下所示的 UI(附图片)。我得到了页眉、页脚和中间区域以及图像视图。
根据图片:区域 A 和区域 C 的高度相似(屏幕的 20%),图像视图应始终放置在区域 B 和区域 C 的中间。
我现在的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>
我给了
marginBottom="40dp"
图像视图将其向上推,这不是一个好的做法。将图像视图放置在区域 B 和区域 C 边界中心的最佳方式是什么。
目前我还给了
android:layout_height="120dp"
对于区域 A 和区域 B 的高度,但理想情况下两者都应为屏幕的 20%(每个),如何实现?
使用权重来给出区域和框架布局的确切部分 例如: weight sum =4 必须在parent Linear layout中给出 A区送1 b区给2 c区给1
在B区后添加图片View with top -(你希望出现在B区的高度)
您可以为每个 A 和 C 赋予 20% 的权重。这样他们将分别占据顶部和底部的 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>
结果 :- 我用不同的颜色代码为不同的部分着色。