将视图添加到指定位置的LinearLayout

Joe*_*dev 52 android android-linearlayout

我有以下带有LinearLayout的main.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>    
    <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>
Run Code Online (Sandbox Code Playgroud)

我在运行时通过代码向LinearLayout添加一个图像

            ImageView image = new ImageView(this);
            image.setImageBitmap(bmp);
            LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
            ll.addView(image);  
Run Code Online (Sandbox Code Playgroud)

但是,我想在LinearLayout中的2个TextView之间添加ImageView.我似乎无法在Android文档中找到一种方法在另一个视图之前或之后添加视图.我怎样才能做到这一点?

NB我电话

setContentView(R.layout.main);
Run Code Online (Sandbox Code Playgroud)

我将ImageView添加到LinearLayout之前.

ant*_*nyt 84

将a添加View到a时ViewGroup,可以指定一个索引,用于设置父视图中视图的位置.

您有两个视图,因此(从零开始计算)您希望在第一个位置添加; 只是打电话ll.addView(image, 1);让它放在两者之间TextViews.


Idi*_*tic 11

文档声明您可以使用索引将其插入所需的位置.我看到你只使用了视图的签名,你是否尝试了带索引参数的签名?

public void addView(View child, int index) 
Run Code Online (Sandbox Code Playgroud)