[英]Android layout: place a view between two views
I am trying to make a screen, preferably with XML only, to look like this:我正在尝试制作一个屏幕,最好只使用 XML,看起来像这样:
I followed this approach, but using FrameLayout I only have the option to position the "orange" view using layout_gravity
, and as I try to explain in the image, I don't know the layouts' height, because both measure with layout_weight.我遵循了这种方法,但是使用 FrameLayout 我只能选择 position 使用layout_gravity
的“橙色”视图,正如我试图在图像中解释的那样,我不知道布局的高度,因为两者都使用 layout_weight 进行测量。
My layout:我的布局:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="onit.radiolisten.MainActivity">
<FrameLayout
android:id="@+id/layout_container_activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TOP LAYOUT, THE GREEN ONE ON THE IMAGE -->
<LinearLayout
android:id="@+id/layout_top_activity_main"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:background="@color/colorPrimary"
android:orientation="vertical"></LinearLayout>
<!-- BOTTOM LAYOUT, THE BLUE ONE ON THE IMAGE -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e6e6e6">
</RelativeLayout>
</LinearLayout>
<!-- THIS IS THE IMAGE I WANT TO POSITION -->
<ImageView
android:id="@+id/btn_play_radio"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="@drawable/play_icon" />
</FrameLayout>
</LinearLayout>
Can this be done with XML only?这只能用 XML 来完成吗?
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--main layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<--top layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="0.4"
android:gravity="bottom|center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@color/callingscreen_color"
android:gravity="center|top" />
</LinearLayout>
<--layout for img-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center|top">
<--IMage u want to put-->
<ImageView
android:id="@+id/ivimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I came here looking for answers to the same question but I found nothing as well as other posts.我来这里是为了寻找同一个问题的答案,但除了其他帖子,我一无所获。 Then, I realized I had to figure a way out myself.然后,我意识到我必须自己想办法。 The below employs the use of weights and margin.下面使用权重和保证金。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?colorSecondary"
android:orientation="vertical"
android:weightSum="11"
tools:context=".ActivityUserAccount">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginBottom="-90dp"
android:background="@drawable/sample_user"
android:elevation="10dp"
android:src="@drawable/sample_user" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|bottom"
android:layout_marginBottom="-50dp"
android:backgroundTint="#FFFAFA"
android:elevation="0dp"
app:cardCornerRadius="50dp">
</androidx.cardview.widget.CardView>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.