2014-11-03 102 views
0

您好,我有自定義的ListView適配器。 我嘗試設置背景圖像,但我無法設置每個項目的高度。 在每個項目我有這個下面的XML佈局:在ListView項目中設置背景

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/listMenuItem" 
    android:layout_width="fill_parent" 
    android:layout_height="80dp" 
    android:background="@drawable/nasi_goreng"> 

    <!-- Thumbnail Image --> 
    <com.android.volley.toolbox.NetworkImageView 
     android:id="@+id/thumbnail" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="8dp" /> 

    <!-- Menu Title --> 
    <TextView 
     android:id="@+id/menuTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:textSize="@dimen/menuTitle" 
     android:textStyle="bold" /> 

    <!-- Post Date --> 
    <TextView 
     android:id="@+id/postDate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/menuTitle" 
     android:layout_marginTop="1dip" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:textSize="@dimen/postDate" /> 

</RelativeLayout> 

但結果是這樣的:

enter image description here

更新: 這裏是我的ListView XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_row_selector"> 
    </ListView> 

</RelativeLayout> 

列表選擇器xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item android:drawable="@drawable/list_row_bg" android:state_pressed="false" android:state_selected="false"></item> 
    <item android:drawable="@drawable/list_row_bg_hover" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/list_row_bg_hover" android:state_pressed="false" android:state_selected="false" ></item> 

</selector> 

如何修復高度?

謝謝

+0

你可以把列表視圖xml文件? – 2014-11-03 08:33:35

+0

請檢查我的問題。我已更新。 – fanjavaid 2014-11-03 08:35:41

+0

你可以把listselector xml? – 2014-11-03 08:40:30

回答

0

嘗試設置RelativeLayout的安卓layout_height的代替的Android = 「WRAP_CONTENT」:layout_height = 「80dp」

請發表您的除外如果它不工作結果

可能是問題來自您的列表選擇器可以發佈您的列表選擇器

+0

我已經嘗試設置'wrap_content',但結果仍然相同。 – fanjavaid 2014-11-03 08:34:12

+0

請檢查我的問題。 – fanjavaid 2014-11-03 08:55:15

0

設置背景圖像對普通視圖的效果不是很好。這種相對佈局在以固定高度繪製此圖像時發生衝突。如果要以更好的性能顯示此圖像,應將ImageView添加到此佈局。

+0

我可以在'imageView'上添加另一個文本嗎? – fanjavaid 2014-11-03 08:40:17

+0

這意味着我們不應該將背景添加到父級佈局? – SilentKiller 2014-11-03 08:41:26

+0

@fanjavaid你可以在RelativeLayout或FrameLayout中做到這一點。它使得ImageView和TextView的背景一樣。 – Yaco 2014-11-06 13:12:26

0

的android:layout_width &安卓:layout_height應該DP在這種情況下指定,否則爲背景圖像需要將採取儘可能多的空間...

+0

它已經在「dp」中指定 – SilentKiller 2014-11-03 08:50:29

+0

看來,listview並不尊重這種規範。它需要儘可能多的空間,因爲它需要.... 你可以嘗試指定dp爲每個項目,而不是佈局本身,如果這有什麼區別... – Jahid 2014-11-03 11:02:07

1

設計根據您的需要的背景圖像。確定列表項的大小(寬度爲&高度),並設計該大小的圖像,然後將其用作背景圖像。如果你想在標籤中使用這個應用程序,然後設計圖像的多個尺寸(根據Android圖像縮放)

閱讀此鏈接(http://developer.android.com/guide/practices/screens_support.html)瞭解更多詳情。

以下是圖像資源的必需指南。

注意:對於其他屏幕密度,android更喜歡hdpi,xhdpi,xxhdpi,xxxhdpi。

所以我們可以考慮爲每個圖標和圖像縮放比例爲2:3:4:6:8。

  • MDPI:2×(基準線)
  • 華電國際:3X
  • xhdi:4倍
  • xxhdi:雙月刊
  • xxxhdpi:8倍速
+0

但寬度如何?因爲每個設備都有不同的寬度。 – fanjavaid 2014-11-03 08:59:47

+0

我已更新我的答案,請參閱並嘗試。如果它幫助你,請接受答案.. :) – 2014-11-03 09:15:59

0

這裏是從androidhive爲例,其工作原理預計:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="20dp" 
    android:background="@color/feed_bg" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="@dimen/feed_item_margin" 
     android:layout_marginRight="@dimen/feed_item_margin" 
     android:layout_marginTop="@dimen/feed_item_margin" 
     android:background="@drawable/bg_parent_rounded_corner" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/feed_item_padding_top_bottom" 
     android:paddingTop="@dimen/feed_item_padding_top_bottom" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 

      <com.android.volley.toolbox.NetworkImageView 
       android:id="@+id/profilePic" 
       android:layout_width="@dimen/feed_item_profile_pic" 
       android:layout_height="@dimen/feed_item_profile_pic" 
       android:scaleType="fitCenter" > 
      </com.android.volley.toolbox.NetworkImageView> 

      <LinearLayout 
       android:layout_width="160dp" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 

       <TextView 
        android:id="@+id/name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textSize="@dimen/feed_item_profile_name" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/timestamp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="@color/timestamp" 
        android:textSize="@dimen/feed_item_timestamp" /> 
      </LinearLayout> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:focusable="false" 
       android:background="@drawable/sharebutton" /> 

     </LinearLayout> 

     <TextView 
      android:id="@+id/txtStatusMsg" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="5dp" 
      android:paddingLeft="@dimen/feed_item_status_pad_left_right" 
      android:paddingRight="@dimen/feed_item_status_pad_left_right" 
      android:paddingTop="@dimen/feed_item_status_pad_top" /> 

     <TextView 
      android:id="@+id/txtUrl" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:linksClickable="true" 
      android:paddingBottom="10dp" 
      android:paddingLeft="@dimen/feed_item_status_pad_left_right" 
      android:paddingRight="@dimen/feed_item_status_pad_left_right" 
      android:textColorLink="@color/link" /> 

     <info.androidhive.listviewfeed.FeedImageView 
      android:id="@+id/feedImage1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:scaleType="fitXY" 
      android:visibility="visible" /> 
    </LinearLayout> 

</LinearLayout> 

請參閱profilepic部分。它的大小,即,高度和寬度是固定的,它的工作原理...

因此,如何分離具有不同的佈局與FILL_PARENT和WRAP_CONTENT PARAMS的ImageView的同時ImageView的PARAMS固定DP的

相關問題