Android: 如何在滚动视图中添加图像,然后在其下方添加文本?

Android: How to add Image and then text below it in scroll view?

大家好,我正在尝试构建布局,但我无法理解如何构建它 我想做的是添加一个图像,其高度和宽度将是屏幕大小,下面是关于图像的描述,用户可以在滚动它时查看该图像,但整个屏幕将仅由图像组成,直到用户滚动下面的文字将不会显示

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    android:fillViewport="true"

    >

<RelativeLayout
    android:layout_weight="1"
    android:id="@+id/relativla"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    >
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"

        android:background="@drawable/hamburger"
        android:id="@+id/navi"
        android:padding="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/descrpitiondietplan"
        android:id="@+id/textView5"
        android:layout_below="@+id/imageView"
         />


</RelativeLayout>

</ScrollView>

你可以在下面看到 link 我想要的布局方式

http://postimg.org/image/avgmk6pi3/

如果您需要任何其他详细信息,请随时发表评论

编辑 1

在尝试了 rachel 解决方案后,我得到了这个

http://postimg.org/image/tdb02gu9l/

现在,正如您在图像中看到的那样,图像视图占据了屏幕的整个宽度,这很好,但是如果我将 match_parent 写入图像视图,它不会占据屏幕的整个高度 layout_height 图片占据了整个屏幕,但它不允许我滚动

听起来您需要做一些工作来获取设备的屏幕尺寸,然后使用它以编程方式设置图像视图的尺寸。

有关计算设备屏幕尺寸的更多详细信息,请在此处查看已接受的答案:Android Layout, view height is equal to screen size

经过对 google 的一些研究,终于让它工作了,Whosebug 终于找到了方法

所以这里首先是您的 activity / 片段

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.second_frag, container, false);


        img = (ImageView) v.findViewById(R.id.imageView);
        Drawable drawable = getResources().getDrawable(R.drawable.dp);
        Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
        float scalingFactor = this.getBitmapScalingFactor(bitmap);

        Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);

        // Set the bitmap as the ImageView source
        this.img.setImageBitmap(newBitmap);

        return v;
    }
    private float getBitmapScalingFactor(Bitmap bm) {
        // Get display width from device
        int displayWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth();

        // Get margin to use it for calculating to max width of the ImageView
        RelativeLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams)this.img.getLayoutParams();
        int leftMargin = layoutParams.leftMargin;
        int rightMargin = layoutParams.rightMargin;

        // Calculate the max width of the imageView
        int imageViewWidth = displayWidth - (leftMargin + rightMargin);

        // Calculate scaling factor and return it
        return ( (float) imageViewWidth / (float) bm.getWidth() );
    }

因为我正在使用片段,所以我有 onCreateView onCreate 将供使用 activity

的人使用

并创建一个 Utils class

public class Util {
    public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
        int scaleHeight = (int) (bm.getHeight() * scalingFactor);
        int scaleWidth = (int) (bm.getWidth() * scalingFactor);

        return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
    }

如果你们有更好的解决方案请告诉我