我有这个 vehicle_list_fragment_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout per il fragment VehicleListFragment. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/recycler_view_fragment_layout"/>
<Button
android:id="@+id/btnAddVehicle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"/>
</LinearLayout>
我的VehicleListFragment
以这种方式使用:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.vehicle_list_fragment_layout, container, false);
btnStartActivity = (Button) rootView.findViewById(R.id.btnAddVehicle);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(getActivity());
setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER);
initAdapter();
mRecyclerView.setAdapter(mAdapter);
setButtonClickListener();
return rootView;
}
问题是RecyclerView
显示正确,但我的btnAddVehicle
未显示。
这是activity_vehicle_layout.xml
,托有我的VehicleListFragment
的Activity
的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="clyky.cartracker.activities.VehicleActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vehicleContainer">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
我使用vehicleContainer
作为片段的容器。预先感谢!
编辑
这是我的recycler_view_fragment_layout
:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout per un fragment che contiene una RecyclerView -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:id="@+id/recyclerView">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
尝试一下。像我在下面添加的那样,基本all向父视图添加权级,并将其layout_weight添加到他们的孩子中,
<?xml version="1.0" encoding="utf-8"?><!-- Layout per il fragment VehicleListFragment. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_weight=".8"
android:id="@+id/recyclerView">
</android.support.v7.widget.RecyclerView>
<Button
android:id="@+id/btnAddVehicle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:text="+" />
</LinearLayout>
也需要向父添加方向,以显示回收器视图和按钮
在vehicle_list_fragment_layout.xml
中尝试一下:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout per il fragment VehicleListFragment. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/recycler_view_fragment_layout"/>
<Button
android:id="@+id/btnAddVehicle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
和recycler_view_fragment_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout per un fragment che contiene una RecyclerView -->
<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="0dp"
android:layout_weight="1"
android:orientation="vertical"
tools:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</LinearLayout>