2017-09-20 42 views
0

我有以下的Relativelayout,其中包含Linearlayout與標籤。此Relativelayout推送到自定義適配器中的自定義ListView。我期望看到Relativelayouts一個接一個,但得到thisListView內部的拉伸佈局

<?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="wrap_content" 
    android:minWidth="25px" 
    android:minHeight="25px" 
    android:padding="8sp"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    <TextView 
     android:text="Водомер 1" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="4sp" 
     android:layout_toRightOf="@id/linearLayout1" 
    <TextView 
     android:text="Последняя поверка:" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/linearLayout1" 
    <TextView 
     android:text="Последние показания:" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/linearLayout1" 
    <TextView 
     android:text="Текущие показания:" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/linearLayout1" 
    </LinearLayout> 
    <ImageView 
     android:src="@android:drawable/ic_menu_gallery" 
     android:layout_width="76sp" 
     android:layout_height="76sp" 
     android:paddingRight="13sp" 
     android:layout_alignParentRight="true" 
</RelativeLayout> 

目標佈局似乎是這樣的。如果我設置爲這個ListView任何其他自定義視圖,它看起來很好。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:minWidth="25px" 
    android:minHeight="25px" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:columnCount="1" 
    app:rowCount="7"> 
    <TextView 
     android:text="Список водомеров" 
     android:layout_width="match_parent" 
     app:layout_rowWeight="0.05" 
     android:gravity="center_horizontal"/> 
    <ListView 
     android:minWidth="25px" 
     android:minHeight="25px" 
     app:layout_rowWeight="0.3" 
     android:layout_width="match_parent" 
     android:choiceMode="singleChoice" /> 
    <TextView 
     android:text="Введите показания водомера" 
     app:layout_rowWeight="0.02" 
     android:layout_width="match_parent" 
     android:gravity="center" /> 
    <EditText 
     android:inputType="number" 
     app:layout_rowWeight="0.04" 
     android:layout_width="match_parent"/> 
    <Button 
     android:text="Сделать фото" 
     android:textSize="20dp" 
     app:layout_rowWeight="0.02" 
     android:layout_width="match_parent" 
     android:gravity="center"/> 
    <Button 
     android:text="Отправить показания" 
     android:textSize="24dp" 
     app:layout_rowWeight="0.15" 
     android:layout_width="match_parent" 
     android:gravity="center"/> 
    <Space 
     app:layout_rowWeight="0.3" /> 
</android.support.v7.widget.GridLayout> 

我該如何解決這個問題?

+1

爲什麼不使用recyclerView,這是最適合您的問題解決方案。試着看看[這個](https://www.androidhive.info/2016/01/android-working-with-recycler-view/),你會發現它。 –

+0

我認爲問題是由ListView上方的TextView引起的。您應該在開發人員選項中啓用「顯示佈局綁定」功能,以便您可以看到導致問題的視圖。訪問這裏瞭解如何啓用「顯示佈局綁定」:https://www.youtube.com/watch?v = GOqJhK0FTjw。希望它有幫助 –

+0

@LGGioan是的,它是提示。所以現在看起來[像](https://ibb.co/ncsp9Q)我的行已被添加到TextView中,而不是ListView – NisuSan

回答

0

爲了解決我的問題,我用LQ Gioan的意見,並與weightSum屬性替換目標網格佈局到的LinearLayout。在內容元素中,我使用layout_weight屬性。

<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="100"> 
<TextView 
     android:text="Список водомеров" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:layout_weight="11" 
     android:id="@+id/textView1" /> 
0

設置列表上方的的TextView的高度wrap_content