2014-05-12 57 views
0

如果我靜態運行此代碼,即只是創建一個示例程序,並做setcontentview它效果很好。imageview問題在相對佈局

但是,如果我膨脹此視圖並顯示爲列表視圖項目。圖像視圖不會延伸。我需要在寬度爲10dp的最左角出現一條黑色條,它應該像線一樣填充視圖。爲每個項目。

另外請注意,如果我這樣做android:layout_height =「?android:attr/listPreferredItemHeight」這樣我的項目佈局,它工作正常。但是項目內容沒有完全顯示,它會在特定的高度被截斷。

我誇大我的看法是這樣

convertView = mLayoutInflater.inflate(com.meganext.googletaskclient.R.layout.layout_list_item, parent, false); 

這裏是我的佈局,我敢膨出的ListView

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="5dp"> 

    <ImageView 
     android:layout_width="10dp" 
     android:layout_height="fill_parent" 
     android:adjustViewBounds="false" 
     android:layout_marginTop="2dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:id="@+id/ID_task_item_priority" 

     android:src="@android:color/black"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Tasks TitleTasks TitleTasks TitleTasks Title Tasks TitleTasks TitleTasks Title" 
     android:id="@+id/ID_task_item_title" 
     android:layout_toRightOf="@+id/ID_task_item_priority" 
     android:singleLine="true" 
     android:ellipsize="end" 

     android:layout_marginRight="40dp" 
     android:textSize="25sp" 
     /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/ID_task_item_notes" 
     android:layout_below="@+id/ID_task_item_title" 
     android:layout_toRightOf="@+id/ID_task_item_priority" 
     android:maxLines="3" 
     android:ellipsize="end" 
     android:text="Task descriptionTask descriptionTask descriptionTask descriptionTask descriptionTask descriptionTask descriptionTask description" 
     android:layout_marginRight="4dp" 
     android:textSize="15sp" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/id_due_date" 
     android:layout_below="@+id/ID_task_item_notes" 
     android:layout_alignParentRight="true" 
     android:layout_marginTop="5dp" 
     android:text="10-2-2014" 
     android:textSize="15sp" 
     /> 

     <CheckBox 
      android:id="@+id/ID_task_item_is_completed_checkbox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/ID_task_item_title" 
      android:layout_alignBottom="@+id/ID_task_item_title" 
      android:layout_alignParentRight="true" 
      android:focusable="false" 
      android:layout_marginRight="4dp" /> 

</RelativeLayout> 

我的列表視圖代碼是

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


    <ListView 
     android:layout_width="fill_parent" 
     android:adjustViewBounds="true" 
     android:layout_height="fill_parent" 
     android:id="@+id/home_activity"> 
    </ListView> 


</RelativeLayout> 

輸出。

enter image description here

+0

寬度'10dp'你要的高度是'10dp'呢? – Raghunandan

+0

不,我需要高度爲fill_parent,那應該是列表項高度。 – Naruto

+0

@Raghunandan,如果我這樣做android:layout_height =「?android:attr/listPreferredItemHeight」這樣的我的項目佈局,它的工作正常。但是項目內容沒有完全顯示,它會在特定的高度被截斷。 – Naruto

回答

0
// try this way,hope this will help you... 

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

    <ImageView 
     android:layout_width="10dp" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:id="@+id/ID_task_item_priority" 
     android:src="@android:color/black"/> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:gravity="right" 
     android:padding="5dp"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Tasks TitleTasks TitleTasks TitleTasks Title Tasks TitleTasks TitleTasks Title" 
      android:id="@+id/ID_task_item_title" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:textSize="25sp"/> 

     <TextView 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/ID_task_item_notes" 
      android:maxLines="3" 
      android:ellipsize="end" 
      android:layout_marginTop="5dp" 
      android:text="Task descriptionTask descriptionTask descriptionTask descriptionTask descriptionTask descriptionTask descriptionTask description" 
      android:textSize="15sp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/id_due_date" 
      android:layout_marginTop="5dp" 
      android:text="10-2-2014" 
      android:textSize="15sp"/> 
    </LinearLayout> 
    <CheckBox 
     android:id="@+id/ID_task_item_is_completed_checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:layout_margin="5dp"/> 
</LinearLayout> 
+0

好吧,我會試試這種方式,讓你知道。謝謝哈里斯。想知道,爲什麼我們不能使用imageview裏面的relativelayout ?,爲什麼顏色不會填充,雖然我們指定fill_parent那裏? – Naruto

+0

您可以使用LinearLayout輕鬆管理您的需求,如果RelativeLayout不是必需的,然後使用as上面,否則我也嘗試使用RelativeLayout實現您的需求,但RelativeLayout是相當難以理解的。 –

+0

好吧,haresh,謝謝,我一定會測試你的佈局並讓你知道,對我來說相對佈局並不是強制性的,謝謝Haresh。找到解決方案的相對佈局,請讓我知道,你是否正確地解決了我的問題?你可以​​很容易地複製一個樣本例子樂。感謝您的支持兄弟 – Naruto