线性布局内部不显示按钮

时间:2017-06-03 15:05:15

标签: android xml android-layout

我有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.xmlActivity的布局,其中包含我的VehicleListFragment

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

2 个答案:

答案 0 :(得分:0)

试试这个。 Basicall将权重添加到父视图和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>

需要将Orientation添加到父级以及垂直显示Recycler视图和按钮

答案 1 :(得分:0)

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>