我已經嘗試了5種不同的方法,試圖讓按鈕顯示在地圖活動上,但無論我怎么嘗試,似乎都無法讓它們顯示出來,有人知道我做錯了什么嗎?這與android studio有關
以下是 activity_maps.xml 中的代碼
<FrameLayout。
xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:map="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:id="@ id/currentloca"。
android:layout_height="match_parent"。
android:layout_width="match_parent"。
android:backgroundTint="#818181"。
android:orientation="vertical"
android:visibility="visible">>
<fragment
android:id="@ id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"。
android:layout_width="match_parent"。
android:layout_height="553dp"。
tools:context=".MapsActivity"/span> />
<androidx.appcompat.widget.LinearLayoutCompat。
android:layout_width="match_parent"
android:layout_height="wrap_content"。
android:layout_gravity="bottom"。
android:orientation="horizontal"/span>>
<Button
android:id="@ id/zoomin"
android:layout_width="205dp"/span>
android:layout_height="wrap_content"。
android:onClick="onZoom"。
android:text="Zoom In" />
<Button
android:id="@ id/zoomout"
android:layout_width="205dp"。
android:layout_height="wrap_content"。
android:onClick="onZoom"。
android:text="Zoom Out" />
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>/span>
uj5u.com熱心網友回復:
正如評論中所建議的,我認為你需要的是一個相對布局。這里有一個例子。
<RelativeLayout。
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>>
<fragment
android:id="@ id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"。
android:layout_width="match_parent"。
android:layout_height="553dp"。
tools:context=".MapsActivity"/span> />
<LinearLayout
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"。
android:layout_alignParentLeft="true"。
android:layout_width="wrap_content"
android:layout_height="wrap_content"/span>>
<Button />
<Button />>
</LinearLayout>/span>
</RelativeLayout>/span>
這樣,按鈕將出現在地圖片段的頂部。
CodePudding
你應該使用FrameLayout或者它的同類,而不是LinearLayout。下面只是展示了一個解釋的結構:
<FrameLayout。
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>>
<fragment
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span> />
<LinearLayout
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"。
android:orientation="horizontal"/span>>
<Button />
<Button />>
</LinearLayout>/span>
</FrameLayout>/span>
LinearLayout將其子視圖鋪設在一條水平或垂直線上。而FrameLayout則是將其子視圖覆寫在上面。
關于你的實際代碼,首先,替換(覆寫)周圍(最外層)的LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:map="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"/span>
android:id="@ id/currentloca"。
android:layout_width="match_parent"。
android:layout_height="match_parent"。
android:backgroundTint="#818181"。
android:orientation="vertical"。
android:visibility="visible">
(...)
</LinearLayout>
用FrameLayout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:map="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"/span>
android:id="@ id/currentloca"。
android:layout_width="match_parent"。
android:layout_height="match_parent"。
android:backgroundTint="#818181"。
android:orientation="vertical"。
android:visibility="visible">
(...)
</FrameLayout>
uj5u.com熱心網友回復:
從你的代碼中移除第一個父級LinearLayout。
更新后的代碼應該是這樣的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:id="@ id/currentloca"。
android:layout_width="match_parent"。
android:layout_height="match_parent"。
android:backgroundTint="#818181"。
android:orientation="vertical"
android:visibility="visible">>
<FrameLayout。
android:layout_width="match_parent"/span>
android:layout_height="555dp"/span>>
<fragment
android:id="@ id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"。
android:layout_width="match_parent"。
android:layout_height="553dp"。
tools:context=".MapsActivity"/span> />
<androidx.appcompat.widget.LinearLayoutCompat。
android:layout_width="match_parent"
android:layout_height="wrap_content"。
android:layout_gravity="bottom"。
android:orientation="horizontal"/span>>
<Button
android:id="@ id/zoomin"
android:layout_width="205dp"/span>
android:layout_height="wrap_content"。
android:onClick="onZoom"。
android:text="Zoom In" />
<Button
android:id="@ id/zoomout"
android:layout_width="205dp"。
android:layout_height="wrap_content"。
android:onClick="onZoom"。
android:text="Zoom Out" />
</androidx.appcompat.widget.LinearLayoutCompat>
</FrameLayout>/span>
</RelativeLayout>/span>
如果你希望你的視圖有可能重疊,請使用FrameLayout。如果你想讓它們線性地顯示,請使用LinearLayout
uj5u.com熱心網友回復:
好吧,你應該使用ConstriantLayout和LinearLayout。
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:map="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:orientation="vertical"。
tools:context=".Activities.Maps"/span>>
<fragment
android:id="@ id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"。
android:layout_width="match_parent"。
android:layout_height="match_parent"/span> />
<LinearLayout
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"。
android:layout_gravity="bottom"。
android:orientation="hizont"
map:layout_constraintEnd_toEndOf="parent"。
map:layout_constraintStart_toStartOf="father"
map:layout_constraintTop_toTopOf="parent">
<Button
android:id="@ id/zoomin"
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:layout_margin="8dp"。
android:layout_weight="1"。
android:onClick="onZoom"。
android:text="Zoom In" />
<Button
android:id="@ id/zoomout"
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:layout_margin="8dp"。
android:layout_weight="1"。
android:onClick="onZoom"。
android:text="Zoom Out" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>/span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313078.html
標籤:
