2015-07-01 50 views
0

我想開發功能類似這樣的如何設置ListView的底端動態

enter image description here

當任何項目被添加,然後底部RelativeLayoutVISIBLE 如果未選擇任何項目,那麼它GONE

問題: 我有這樣的問題: enter image description here

圖1: 代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="50dp" 
    android:background="@android:color/white" > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
    </ListView> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentBottom="true" > 

     <View 
      android:id="@+id/sprator" 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:background="@android:color/black" /> 

     <ImageView 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_margin="10dp" 
      android:src="@drawable/icon" /> 

     <ImageView 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentRight="true" 
      android:layout_margin="10dp" 
      android:src="@drawable/icon" /> 
    </RelativeLayout> 

</RelativeLayout> 

當沒有項目被選中,那麼底部RelativeLayoutGONE圖像2

當任何項目被添加然後底部RelativeLayoutVISIBLE圖3 3

+0

ü上啓動,然後代碼張貼代碼 –

回答

1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@+id/productList" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
    <RelativeLayout 
     android:id="@+id/relLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     android:layout_alignParentBottom="true" > 
     <View 
      android:id="@+id/sprator" 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:background="@android:color/black" /> 
     <ImageView 
      android:id="@+id/icoShopping" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_margin="10dp" 
      android:src="@drawable/icon" /> 

     <ImageView 
      android:id="@+id/icoCheckout" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_alignParentRight="true" 
      android:layout_margin="10dp" 
      android:src="@drawable/icon" /> 
    </RelativeLayout> 
</LinearLayout> 
1

將其更改爲LinearLayout ,如下面的僞XML:

<LinearLayout> 
    <ListView layout_weight="1" layout_height="0dp" /> 
    <RelativeLayout layout_height="wrap_content" /> // bottom layout 
</LinearLayout> 
2

將layout_above屬性添加到列表中。在xml中,它必須位於底部佈局下方,否則將不會聲明您給該佈局的標識(在我的示例中爲resultLayout)。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_marginTop="50dp" 
android:background="@android:color/white" > 

<RelativeLayout 
    android:id="@+id/confirmLayout" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" > 

    <View 
     android:id="@+id/sprator" 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:background="@android:color/black" /> 

    <ImageView 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_margin="10dp" 
     android:src="@drawable/icon" /> 

    <ImageView 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_alignParentRight="true" 
     android:layout_margin="10dp" 
     android:src="@drawable/icon" /> 
</RelativeLayout> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/confirmLayout" > 
</ListView> 
</RelativeLayout> 
+0

這是正確的答案 – Tomislav

0

放置RelativeLayout

上述ListView實現您的活動OnItemClickListener。然後當用戶選擇ro取消選擇項目時,將RelativeLayout的可見性更改爲View.VSIBLEView.GONE

0

首先,爲您的內部相對佈局給出一個id。

然後,使用setOnItemClickListener來捕獲點擊事件。

查找您的活動代碼佈局,使用:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_id); 

然後,您可以用改變的可視性:

layout.setVisibility(RelativeLayout.VISIBLE);