Подтвердить что ты не робот

Добавление представления к LinearLayout в указанной позиции

У меня есть следующий файл main.xml с LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1" android:id="@+id/llid">
    <TextView android:text="Client profile"
    android:id="@+id/ProfileName"
    android:layout_width="fill_parent"
    android:textStyle="bold"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">
    </TextView>    
    <TextView android:text="Specs"
    android:id="@+id/Specs"
    android:layout_width="fill_parent"
    android:textStyle="bold"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">
    </TextView>
</LinearLayout>

Я добавляю изображение в LinearLayout через код во время выполнения таким образом

            ImageView image = new ImageView(this);
            image.setImageBitmap(bmp);
            LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
            ll.addView(image);  

Однако я хочу добавить ImageView между 2 TextViews в моем LinearLayout. Я не могу найти способ в документах Android для добавления представления перед другим представлением или после него. Как я могу это сделать?

NB Я звоню

setContentView(R.layout.main);

До Я добавляю ImageView в LinearLayout.

4b9b3361

Ответ 1

При добавлении View в ViewGroup вы можете указать индекс, который устанавливает положение представления в родительском.

У вас есть два вида и так (отсчет с нуля), которые вы хотели бы добавить на 1-ю позицию; просто позвоните ll.addView(image, 1);, чтобы он помещался между двумя TextViews.

Ответ 2

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

public void addView(View child, int index) 

Ответ 3

У меня возникла аналогичная проблема. В моем случае я хотел добавить LinearLayout в последнюю позицию другого LinearLayout. Чтобы выполнить это, я сделал:

LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// add others views to your linear layout

parentLayout.addView(layout, parentLayout.getChildCount());

Ответ 4

setContentView(R.layout.main);

ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);

и в xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:id="@+id/llid">
<TextView android:text="Client profile"
android:id="@+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>    
<ImageView android:id="@+id/imagefield" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</ImageView> 
<TextView android:text="Specs"
android:id="@+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>

Ответ 5

Добавьте ImageView в xml, и если он не используется, сделайте его невидимым (image.setVisibility(View.INVISIBLE)). Он может ничего не показывать, если изображение не установлено.

Ответ 6

Чтобы получить позицию зрения в группе представлений

                val targetPosition = oldLL.indexOfChild(viewToAdd)

Чтобы добавить представление в позицию в группе представлений

                newLL.addView(viewToAdd,targetPosition)