2014-12-28 67 views
0

我想寫一個佈局,其中將包含Android佈局中的列表視圖和按鈕。列表視圖是一個片段,按鈕是佈局的一部分。添加列表片段時隱藏Android的按鈕元素

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

    <!-- fragment container to display transactions for this budget --> 
    <LinearLayout 
     android:id="@+id/transactionsContainer" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/newTransaction" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/button_new_transaction"/> 

</LinearLayout> 

listview_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:padding="10dp" 
       android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/transactionTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="6dp" 
     android:layout_toLeftOf="@+id/transactionAmount" 
     android:layout_toStartOf="@+id/transactionAmount" 
     style="@style/ListItemDescription"/> 

    <TextView 
     android:id="@+id/transactionAmount" 
     style="@style/AmountText" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentTop="true"/> 

</RelativeLayout> 

我加入這樣的片段;

final FragmentManager fm = getFragmentManager(); 
Fragment container = fm.findFragmentById(R.id.transactionsContainer); 

if (container == null) { 
    final TransactionListFragment fragment = new TransactionListFragment(); 
    fm.beginTransaction().add(R.id.transactionsContainer, fragment).commit(); 
} 

該片段被添加到佈局很好,但是,當列表增長大於顯示高度的按鈕丟失。如果有少量的列表視圖項目,則該按鈕是可見的;

按鍵採用3項

enter image description here

按鍵採用10項

enter image description here

當調試與hierarchyviewer認爲缺少可見,按鈕是存在的,但長相就好像它在listview片段後面一樣;

enter image description here

任何想法是怎麼回事?

回答

1

使用layout_weight屬性

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <!-- fragment container to display transactions for this budget --> 
    <LinearLayout 
     android:id="@+id/transactionsContainer" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="0dp" /> 

    <Button 
     android:id="@+id/newTransaction" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/button_new_transaction"/>a 

</LinearLayout> 
+0

感謝您的建議,但是嘗試此操作時,列表視圖中只顯示一個項目。在閱讀了[ScrollView](http://developer.android.com/reference/android/widget/ScrollView.html)文檔後,它說你不應該使用帶有ListView的ScrollView,因爲ListView需要自己滾動 – Kevin

+0

@凱文,是的,這是真的,那麼你有另一種選擇。查看更新。按鈕將始終在底部,列表視圖將滾動'transactionsContainer'內 – gio

+0

感謝您的更新,這幾乎是我在尋找。我不希望它位於屏幕底部的固定位置,而是希望它出現在佈局的底部 - 這樣在滾動完成後就可以看到它。有沒有辦法讓碎片成爲線性佈局的第一個孩子?如果是這樣,我可以添加按鈕作爲LinearLayout的一部分。 – Kevin

0

給一個權重佈局transactionsContainer,讓你迫使它不填寫許多項目的情況下,整個高度,並將其包裝在一個滾動型通過項目滾動而有他們中的很多:

transactionsContainer.addView(view, 0); 
0:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="10"> 

    <!-- fragment container to display transactions for this budget --> 
    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
    <LinearLayout 
     android:id="@+id/transactionsContainer" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/newTransaction" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="button_new_transaction"/> 
    </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

,並在運行時添加視圖的滾動型時

+0

這種作品。向容器和按鈕添加一個權重確實會顯示該按鈕,但它的位置固定在屏幕的底部(不管是否滾動)。我希望它在滾動結束後出現。 – Kevin

+0

所以,只需從佈局中刪除按鈕,動態地通過運行時將其添加爲滾動型/的ListView –

+0

也是最後一個項目,你可以試試我的更新答案 –