Android - 使用Picasso的fit()和centerInside()时在ImageView下显示其他布局

时间:2016-02-22 17:50:49

标签: button imageview picasso

下面是一个不能包装内容的ImageView。使用Picasso的fit()和centerInside()属性显示图像时的高度和宽度。

由于这个原因,我必须在dp中设置一个高度或者在match_parent'中设置状态。如果我在dp中设置一个特定的高度,我的按钮会显示,但这当然不适合不同尺寸的屏幕。如果我设置' match_parent' ImageView填满了屏幕的其余部分,我无法在ImageView下面显示我的按钮。

有没有人知道以某种方式嵌套我的ImageView的方法,所以我可以显示我的最终Button?

另外,我无法弄清楚如何对齐ImageView顶部的图片,如果可以,请帮助解决。

main_activity.xml。

<?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:background="@color/colorBackground">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    style="@style/Toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:minHeight="?attr/actionBarSize" />

<LinearLayout
    android:id="@+id/button_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"
    android:padding="10dp">

    <Button
        android:id="@+id/button_kitty"
        style="@style/Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_kitty" />

    <Space
        android:id="@+id/space"
        android:layout_width="10dp"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_cat"
        style="@style/Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_cat" />

</LinearLayout>

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/button_layout"
    android:layout_marginBottom="17dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="4dp"
    android:adjustViewBounds="true" />

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button_layout"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:indeterminate="true"
    android:visibility="invisible" />

<Button
    android:id="@+id/button_facebook"
    style="@style/Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/image"
    android:layout_centerHorizontal="true"
    android:gravity="center" />

ActivityMain.java

@Override
public void onResponse(Call<KittyCatModel> call, Response<KittyCatModel> response) {
    KittyCatModel model = response.body();
    String url = model.getSource();
    Picasso.with(this)
            .load(url)
            .fit()
            .centerInside()
            .into(imageView, this);
}

Screen shot with Layout Bounds on

2 个答案:

答案 0 :(得分:0)

在Picasso的github上,有一个关于fit()的线程在imageView的adjustViewBounds设置为true时无效。删除任何一个并看看它是怎么回事(我是从移动设备上做的,所以在回答之前无法测试它,抱歉)。

这是该线程的链接: https://github.com/square/picasso/issues/425

该线程链接到Picasso的git上另一个关于创建自定义imageView以使其按照您想要的方式工作的线程

这是该线程的链接: https://github.com/square/picasso/issues/457

答案 1 :(得分:0)

对此问题有一些离线帮助,答案是:

这与Picasso无关,它只是一个XML问题。

ImageView和ProgressBar需要位于a的RelativeLayout中 LinearLayout所以我可以将layout_weight =“1”应用于RelativeLayout。

这允许我在我的ImageView宽度和高度上“match_parent” 图像填充其布局但不隐藏底部按钮。

希望将来帮助其他人。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_background"
    android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    style="@style/Toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:minHeight="?attr/actionBarSize" />

<LinearLayout
    android:id="@+id/button_layout_generate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"
    android:orientation="horizontal"
    android:padding="12dp">

    <Button
        android:id="@+id/button_kitty"
        style="@style/Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_kitty" />

    <Space
        android:id="@+id/space"
        android:layout_width="10dp"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_cat"
        style="@style/Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_cat" />

</LinearLayout>

<RelativeLayout
    android:id="@+id/image_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@id/button_layout_generate"
    android:layout_weight="1"
    android:paddingRight="16dp"
    android:paddingLeft="16dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:indeterminate="true"
        android:visibility="invisible" />

</RelativeLayout>

<LinearLayout
    android:id="@+id/button_layout_facebook"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/image"
    android:orientation="horizontal"
    android:padding="12dp">

    <Space
        android:id="@+id/space_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5" />

    <Button
        android:id="@+id/button_facebook"
        style="@style/Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/image"
        android:layout_weight="1"
        android:enabled="false"
        android:gravity="center"
        android:text="@string/button_facebook" />

    <Space
        android:id="@+id/space_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5" />

</LinearLayout>

</LinearLayout>
相关问题