2014-05-12 56 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