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

时间:2011-08-02 19:29:12

标签: 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>

我在运行时通过代码将图像添加到LinearLayout,如此

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

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

NB 我致电

setContentView(R.layout.main);

之前我将ImageView添加到LinearLayout。

6 个答案:

答案 0 :(得分:73)

View添加到ViewGroup时,您可以specify an index设置父级中视图的位置。

您有两个视图,因此(从零开始计算)您希望在第一个位置添加;只需致电ll.addView(image, 1);即可将其置于两个TextViews之间。

答案 1 :(得分:10)

文档说明您可以使用索引将其插入到您想要的位置。我看到你只使用了视图的签名,你是否尝试使用索引参数签名?

public void addView(View child, int index) 

答案 2 :(得分: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());

答案 3 :(得分:2)

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>

</LinearLayout>

答案 4 :(得分:1)

将一个ImageView添加到xml中,如果没有使用它,则使其不可见(image.setVisibility(View.INVISIBLE))。当没有设置图像时,它可能无法显示任何内容。

答案 5 :(得分:0)

获取视图在视图组中的位置

                val targetPosition = oldLL.indexOfChild(viewToAdd)

在视图组中的某个位置添加视图

                newLL.addView(viewToAdd,targetPosition)
相关问题