繁体   English   中英

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>

您可以在下面的链接中看到我想要的布局

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

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

编辑1

尝试瑞秋解决方案后,我得到这个

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

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

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

有关计算设备屏幕尺寸的更多详细信息,请参见此处接受的答案: Android布局,视图高度等于屏幕尺寸

经过对Google和Stackoverflow的一些研究后,终于使它工作了

所以这首先是你的活动/片段

@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将适合使用活动的人

并创建一个Utils类

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);
    }

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

暂无
暂无

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

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