java - 如何设置图片大小正方形.. 所有手机屏幕都支持哪些?

标签 java android android-layout user-interface

项目布局

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_panel"
        android:background="@drawable/transback"
        app:cardCornerRadius="10dp"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp">

    <ImageView
        android:id="@+id/categoryImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

    </android.support.v7.widget.CardView>

我想像这样设置图像大小正方形 屏幕宽度 = 图片高度

这是我用于回收站查看的项目

这怎么可能……?

最佳答案

可以自定义ImageView来创建SquareImageView

public class SquareImageView  extends ImageView {

  public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}


  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    setMeasuredDimension(width, width);
  }

}

关于java - 如何设置图片大小正方形.. 所有手机屏幕都支持哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55256841/

相关文章:

java - 我可以使用 mime 类型 application/msword 将页码添加到动态生成的文档中吗?

Android ListView 行填充/边距

安卓:比较日期

android - CardView 角在 AlertDialog 中不圆

java - 在 Android 中使用 App Combat 主题和 ListView

java - Android 版 OpenCV 中的矩阵乘法

android - 布局在图形布局和实际设备上出现不同

Android studio 预览与真实屏幕不匹配

Java-单元格更改时如何保存JTable?

android - IPC涉及的应用的UID和应用ID