简体   繁体   中英

Adding ImageButtons to the LinearLayout of a ScrollView programmatically

I am trying to accomplish this in this activity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MantisCharActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:id="@+id/charActivityLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

with this code:

    TypedValue outValue = new TypedValue();
    this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);

    LinearLayout layout = (LinearLayout) findViewById(R.id.charActivityLayout);
    String path = Environment.getExternalStorageDirectory().toString()+"/Download/TestData/";

    for (File file : folder.listFiles()) {

        ImageButton imgbut = new ImageButton(this);

        //image
        Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath() + "/00thumb.jpg");
        imgbut.setImageBitmap(myBitmap);

        imgbut.setMinimumHeight(210);
        imgbut.setAdjustViewBounds(false);
        imgbut.setBackgroundResource(outValue.resourceId);
        imgbut.setCropToPadding(false);
        imgbut.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        layout.addView(imgbut);

The activity stays completely empty. The '00thumb.jpg' files exist and loading them doesn't throw an error so thats probably not the problem. What am i missing or doing wrong?

Edit: It seems like my code worked (even though it wasn't really elegant). Idk what exactly happened, but something was wrong with the emulator i was using. The app was launched when i started it through android studio, but it never updated the build. I created a new device in the emulator and it works now.

do something like this.

    val layout = view.findViewById<LinearLayout>(R.id.yourId)
    view.layoutParams = ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
    layout?.addView(view)

以及添加视图及其LayoutParams之后的layout.requestLayout()

I think problem is: can't load the image, First, You should check to add imgbut is a success or no:

        imgbut.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent))
        val params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            100
        )
        params.topMargin = 10
        params.bottomMargin = 10
        imgbut.layoutParams = params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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