【发布时间】:2020-02-24 15:19:03
【问题描述】:
我在地图活动上添加了导航抽屉。现在我正在尝试在我的家庭活动中添加一个片段,但片段未显示在活动中,但片段类中的代码正在运行。 我的 FrameLayout 是否隐藏在地图片段后面?
我对片段非常陌生。 请帮帮我。
首页类布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constrain_layout"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintHorizontal_bias="0.0"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent"
map:layout_constraintVertical_bias="0.0"
tools:context=".HomeActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段类
public class RideHistory extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Toast.makeText(getActivity(), "Ride History", Toast.LENGTH_SHORT).show();
View view = inflater.inflate(R.layout.fragment_ride_history,container,false);
return view;
}
}
片段 xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".Fragments.RideHistory">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Ride History" />
</FrameLayout>
【问题讨论】:
-
请使用LayoutInspector并检查您的Fragment是否已添加到屏幕(虽然您看不到)由于您的Fragment包含带有文本“RideHistory”的TextView,您可以搜索View树对于带有 TextView 子项的 FrameLayout 以及所需的文本
-
我收到一个错误,布局检查器因超时错误而失败。
-
太糟糕了,它是一个很棒的工具(工作时)。 OK,那么为了查明Fragment是否隐藏在地图后面,可以稍微改变一下Activity的布局文件。比如让地图Fragment有
android:visibility="invisible" -
不工作的地图片段没有可见性选项。
标签: java android xml android-fragments