Добавить изображения на экран программно - PullRequest
0 голосов
/ 04 апреля 2020

В одном из упражнений моего приложения у меня есть кнопка, где я хочу отображать изображение при каждом нажатии кнопки. Например:

enter image description here

При нажатии кнопки моей активности на экране появляется изображение, как показано.

enter image description here

Второй и последующие нажатия на кнопку приведут к добавлению нового изображения соответственно.

enter image description here

Я хотел бы получить несколько предложений о том, как мне этого добиться.

Ответы [ 2 ]

0 голосов
/ 04 апреля 2020

Добавить вертикаль LineaLayout и динамически добавлять виды:

private void createViews() {
        for (int i = 0; i < numberOfViews; i++) {
            view = new ImageView(context);
            int width = 300;
            int height = 50;
            view.setPadding(18, 10, 0, 0);
            view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
            view.setId(i);
            view.setGravity(Gravity.CENTER_VERTICAL);
            view.setBackgroundColor(Color.parseColor("someColor"));
            viewList.add(view);
        }
    }

     Rect rectf = new Rect();
        for (ImageView view : viewList) {
                    view.getGlobalVisibleRect(rectf);
                    coordinates.add(rectf.bottom);
                }
0 голосов
/ 04 апреля 2020

Я сделал что-то похожее на это, но с TextView. В основном я сделал это:

XML:

Для моего случая я сделал TableLayout Пример:

<TableLayout
    android:id="@+id/existedTableLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/margin_standard">

    <TableRow>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/number_text"
            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />
    </TableRow>
</TableLayout>


Активность

Примечание. Измените его на ImageView для вашего случая

/* //Get the TableLayout. Ex:
    private TableLayout existedTableLayout = findViewById(R.id.existedTableLayout);
*/
/* Make onClickListerner to call below function */
private void addTableRowDynamically() {
        //Make new Row
        TableRow newRow= new TableRow(this);

        TextView newNoTextView = new TextView(this);
        //some TextView method, do your research about ImageView
        newNoTextView.setLayoutParams(new TableRow.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        newNoTextView.setText("this is text");
        newNoTextView.setTextAppearance(this, android.R.style.TextAppearance_DeviceDefault_Large);

        // Add the TextView to the newRow
        newRow.addView(newNoTextView);

        // Add the newRow which contain the TextView to the TableLayout, below
        existedTableLayout.addView(newRow, existedTableLayout.getChildCount());
}
...