簡體   English   中英

Android 布局:在兩個視圖之間放置一個視圖

[英]Android layout: place a view between two views

我正在嘗試制作一個屏幕,最好只使用 XML,看起來像這樣: 在此處輸入圖像描述

我遵循了這種方法,但是使用 FrameLayout 我只能選擇 position 使用layout_gravity的“橙色”視圖,正如我試圖在圖像中解釋的那樣,我不知道布局的高度,因為兩者都使用 layout_weight 進行測量。

我的布局:

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

這只能用 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>

我來這里是為了尋找同一個問題的答案,但除了其他帖子,我一無所獲。 然后,我意識到我必須自己想辦法。 下面使用權重和保證金。

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM