繁体   English   中英

如何根据屏幕尺寸更改imageview

[英]How to change imageview according to screen size

我试图在导航标题中添加一个imageview(配置文件图像),2 textview(用户名和电子邮件)。

这是导航标题的xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/header_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@android:drawable/sym_def_app_icon" />

<TextView
    android:id="@+id/header_username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/header_email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>

我通过picasso加载imageview,这是我加载它的代码。

private void setNavHeader() {
    User user = presenter.getUser();
    username.setText(user.getUsername());
    email.setText(user.getEmail());
    try {
        Picasso.with(this)
                .load(user.getUserImage())
                .error(R.drawable.ic_menu_camera)
                .resize(200, 200)
                .into(profileImage);
    }catch(Exception e){
        profileImage.setImageResource(R.drawable.ic_menu_camera);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
        profileImage.setLayoutParams(layoutParams);
    }
}

这是我的输出 在此处输入图片说明 但是,它仅适合我的手机屏幕尺寸。 当我将屏幕尺寸更改为较小的屏幕尺寸时。 个人资料图片将覆盖用户名和电子邮件。 如何根据屏幕尺寸更改个人资料图片的大小,这样不会掩盖用户名和电子邮件?

首先,删除try and catch。 隐藏此错误,仅检查可能的url问题并手动设置图像(如在缓存块中)不是很好。 如果您使用String或Uri,请检查userimage网址是否为null

关键是您使用的是LinearLayout ,它要注意子视图不会重叠。 如果您更改ImageView高度,则可能是因为您的android:layout_height="@dimen/nav_header_height"值定义的空间不足,所以TextViews可能会在底部被剪切。

在XML中设置所需的高度和宽度,例如:

<ImageView
    android:id="@+id/header_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:srcCompat="@android:drawable/sym_def_app_icon" />

只需找到正确的尺寸/询问您的设计师(如果您使用的话)适合该图像的尺寸是多少。 这将改变ImageView的View边界,显示的图像当然取决于此大小。

您可以使用imageView的以下属性进行播放:

android:scaleType
android:adjustViewBounds

这改变了图像在ImageView边界内的显示方式。

使用dp代替像素,这也需要注意,您的图像视图可以在不同的设备尺寸上得到很好的缩放。

您可以使用Picasso功能,例如:

Picasso.with(this)
            .load(user.getUserImage())
            .error(R.drawable.ic_menu_camera)

            .placeholder(...)

            .resize(imageView.getMeasuredWidth(), imageView.getMeasuredHeight()) // <-- is needed for some functions

            .fit()// <--
            .centerCrop()// <--
            .centerInside()// <--

            .into(profileImage);

只需找到可以提供最佳结果的功能即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM