簡體   English   中英

以編程方式將 textview 放在 imageview Android 上

[英]Programatically put textview on imageview Android

我正在嘗試將文本放在 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);

LinearLayout 將視圖一個一個排列,視圖不能疊加。 您可以使用 FrameLayout 將 TextView 重疊在 ImageView 之上。 或者,如果您有許多項目的列表,則可以使用 RecyclerView。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM