2017-06-03 61 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