【问题标题】:Imageview inside relative layout does not stretch full view相对布局内的 Imageview 不会拉伸全视图
【发布时间】:2014-05-13 02:58:44
【问题描述】:

如果我膨胀这个项目并将其显示在我的列表视图中,则图像视图不会填充父级,即它只会拉伸到宽度但不会扩展到视图高度。

   <?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="match_parent"
         android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_marginTop="2dp"
         android:layout_marginLeft="5dp"
         android:layout_marginRight="5dp"
         android:layout_marginBottom="5dp"
         android:id="@+id/ID_task_item_priority"
         android:src="@color/abs__background_holo_dark"/>

    <TextView 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Your title"
         android:id="@+id/ID_task_item_title"
         android:singleLine="true"
         android:ellipsize="end"
         android:layout_toRightOf="@+id/ID_task_item_priority"
         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:maxLines="3"
        android:ellipsize="end"
        android:text="Your note"
        android:layout_toRightOf="@+id/ID_task_item_priority"
        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>

物品图片

【问题讨论】:

  • 你试过添加android:scaleType="fitXY"吗?
  • 在相对布局的imageview中?
  • 在 imageview xml 声明中是的
  • &lt;ImageView android:layout_width="10dp" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="2dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:scaleType="fitXY" android:id="@+id/ID_task_item_priority" android:src="@color/abs__background_holo_dark"/&gt; 试过这样,没有运气:(。imageview 根本没有拉伸到查看高度。
  • 如果我硬编码高度,它会伸展,但如果我给 fill_parent 为什么不呢?

标签: android android-layout listview


【解决方案1】:

您根据设备配置设置图像布局参数运行时

使用此代码将图像视图的宽度和高度设置为与屏幕参数相同

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.rl_main);
    RelativeLayout.LayoutParams lparams= new RelativeLayout.LayoutParams(width,height);

    ImageView iv= (ImageView)findViewById(R.id.imageView1);
    relativeLayout.removeView(iv);
    relativeLayout.addView(iv,lparams);

这可能对你有帮助

【讨论】:

  • 您好,为什么这里的静态代码不起作用,是什么问题?
  • 因为不同的设备有不同的屏幕分辨率,任何图像都没有相同的,所以它不适合我们的父级布局
  • 如果我在 listview 上膨胀视图,那么只会出现问题,但只要我在视图中运行此代码,它就可以正常工作
  • 我想用静态代码实现,我的问题是。如果我膨胀上述布局并提供给listview。图像,即黑色条带不会从上到下填充。它只是停留在最左边的角落,看起来只有 10dp 宽。就是这样。
  • 那是因为你设置了你的imageview的参数android:layout_width=10dp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多