android-discuss icon indicating copy to clipboard operation
android-discuss copied to clipboard

【问答】在页面中,有两张图并排显示,imageview宽度为屏幕的一半,高度自适应。达不到UI的效果

Open Reallym404 opened this issue 10 years ago • 10 comments

qq 20160414225630

上面这个是效果图,两张图,点击后 打开相机拍照,然会后显示在这两个imageview中。但是,分别点击然后返回设置图片后,显示的效果是这样的。 qq 20160414225657 我的这部分布局是这样的: `

        <ImageView
            android:id="@+id/sales_otc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/otc_bg"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_margin="@dimen/space_5"
            android:layout_gravity="center_vertical|left"
            android:onClick="onOtc"
            android:visibility="visible"/>

        <ImageView
            android:id="@+id/sales_card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/card_bg"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:onClick="onCard"
            android:layout_margin="@dimen/space_5"
            android:layout_gravity="center_vertical|right"
            android:visibility="visible"/>
    </LinearLayout>`

各位大神有没有好的方法能实现这个UI效果。能否提供个demo或 代码。。3Q!

Reallym404 avatar Apr 14 '16 15:04 Reallym404

动态用代码设置两个ImageView的宽度 或者用PercentRelativeLayout控件 设置百分比宽度 50%

ByteRax avatar Apr 15 '16 07:04 ByteRax

用weight来做? 2个 image 1 : 1 On Fri, Apr 15, 2016 at 12:30 AM tanranran [email protected] wrote: 动态用代码设置两个ImageView的宽度 或者用PercentRelativeLayout控件 设置百分比宽度 50%

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

LLin233 avatar Apr 15 '16 07:04 LLin233

设置LinearLayout的weightSum=2,ImageView的宽度为0dp,就可以了。 当ImageView的宽度不为0dp的时候,LinearLayout会按照控件声明的尺寸进行分配,然后再将剩下的尺寸按照weight分配。

yydcdut avatar Apr 15 '16 07:04 yydcdut

可能使布局的问题,我改成相对布局然后imageview固定大小就好了。。。

Reallym404 avatar Apr 16 '16 11:04 Reallym404

这样设置在分辨率不同的手机上会有问题。 2016年4月16日 下午7:16,"yeming1001" [email protected]写道:

可能使布局的问题,我改成相对布局然后imageview固定大小就好了。。。

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/android-cn/android-discuss/issues/436#issuecomment-210792638

MatrixMuto avatar Apr 16 '16 16:04 MatrixMuto

@MatrixMuto 没找到 比较好的方法,而且还是自适应的 哎

Reallym404 avatar Apr 18 '16 02:04 Reallym404

@yeming1001 这个很多解决方案,涉及到屏幕适配,之前看到一个解决方案是这样的,对于主流手机都按照UI图的尺寸,分比例计算像素,做适配,有人直接写了一个jar生成这个适配的,看链接鸿洋大神

biaomingzhong avatar Apr 20 '16 02:04 biaomingzhong

     <ImageView
        android:id="@+id/sales_otc"
         android:layout_width="0dp"<!-- 注意是0dp -->
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@mipmap/otc_bg"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_margin="@dimen/space_5"
        android:layout_gravity="center_vertical|left"
        android:onClick="onOtc"
        android:visibility="visible"/>

    <ImageView
        android:id="@+id/sales_card"
        android:layout_width="0dp"<!-- 注意是0dp -->
        android:layout_height="wrap_content"
        android:src="@mipmap/card_bg"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:onClick="onCard"
        android:layout_margin="@dimen/space_5"
        android:layout_gravity="center_vertical|right"
        android:visibility="visible"/>

两张图之间的距离随意,不会影响两个图的比例。在不同的分辨率手机的环境下是相同的效果。

nobShiny avatar May 23 '16 09:05 nobShiny

imageview 一样的。但是填充imageview的内容不同。你把 android:scaleType="fitCenter"换成 android:scaleType="fitXY"试试

NanFengyang avatar Jun 24 '16 14:06 NanFengyang

先确定ui图片的宽高比。 那实现一个自定ImageView就行 重写 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measuredWidth = getMeasuredWidth(); //这里可以根据宽度来设置高度 int height = measuredWidth * 宽高比; setMeasuredDimension(measuredWidth, height ); }

如果想显示图片全部内容用fitCenter, 如果想图片撑满控件用centerCrop,自己调试看效果

Caij avatar Jun 25 '16 03:06 Caij