以编程方式将textview放在imageview Android上

时间:2020-09-03 15:49:48

标签: android bitmap textview android-linearlayout android-imageview

我正在尝试在imageview上放置文本,但是我不知道如何以编程方式进行操作。 首先,我从Firebase存储下载图像,然后将其放置在线性布局中并带有一些文本。由于数据库中存在图像,因此该过程会重复进行多次。

这是我的代码: XML:

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
        </LinearLayout>

    </ScrollView>

以及该活动中的相关代码:

                    fileRef.getFile(localFile)
                            .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                                @RequiresApi(api = Build.VERSION_CODES.Q)
                                @Override
                                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.MATCH_PARENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT);
                                    LinearLayout linearLayout = findViewById(R.id.linearLayout);
                                    Bitmap bitmap = BitmapFactory.decodeFile(finalLocalFile.getAbsolutePath());
                                    TextView textView = new TextView(GaleryActivity.this);
                                    textView.setText("Something");
                                    textView.setTextSize(24);
                                    Typeface typeFace = Typeface.createFromAsset(getAssets(), "aqua.ttf");
                                    textView.setTypeface(typeFace);
                                    textView.setGravity(Gravity.END|Gravity.BOTTOM);
                                    linearLayout.addView(textView);
                                    ImageView imageView = new ImageView(getApplicationContext());
                                    imageView.setBackground(GaleryActivity.this.getResources().getDrawable(R.drawable.mybutton));
                                    imageView.setImageBitmap(bitmap);
                                    linearLayout.addView(imageView,params);

1 个答案:

答案 0 :(得分:0)

LinearLayout逐一排列视图,视图不能覆盖。您可以使用FrameLayout在ImageView顶部重叠TextView。或者,如果您有很多项目的列表,则可以使用RecyclerView。

相关问题