2017-06-03 55 views
1

我有这个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> 
+0

您的按钮在'recycler_view_fragment_layout'布局下。 – Stanojkovic

+0

@Stanojkovic我不这么认为:我用'ScrollView'包裹了我的'vehicle_list_fragment_layout',我的按钮还没有显示出来 – Clyky

+0

检查你的'ScrollView'高度。 – Stanojkovic

回答

1

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

需要添加定位于母公司以及显示回收视图和按钮垂直

+0

感谢您的回答,但您的解决方案不起作用,我的按钮仍然隐形 – Clyky

+0

发布您的recycler_view_fragment_layout –

+0

好吧,我已更新我的帖子 – Clyky

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> 
+0

'layout_weight'不能应用于'include'标签 – Clyky

+0

这是错误的,不能应用重量来包含标签 –

+0

我编辑了我的答案,主意保持不变,为包含的布局添加1的权重。在包含的布局中,'android:layout_height =“0dp”'对于性能和'tools:layout_height =“match_parent”'是有用的,允许您在编辑器中看到布局的呈现。 – Wang