在LinearLayout中指定位置添加视图

56

我有以下带有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中的两个TextView之间添加ImageView。在Android文档中,我似乎找不到在另一个视图之前或之后添加视图的方法。我该如何做?

NB 我称之为

setContentView(R.layout.main);

在将 ImageView 添加到 LinearLayout 之前之前


你为什么要这样做?只需要在布局中声明ImageView并将源设置为它,而不是在代码中插入ImageView。 - user468311
2
这样行不通。在运行时,我正在动态加载一张图片,并希望在我的布局中显示它。 - Joeblackdev
你可以动态地为ImageView设置源。我真的不明白按照你描述的方式这样做的原因。 - user468311
6个回答

94

当将一个View添加到ViewGroup时,您可以指定一个索引来设置视图在父容器中的位置。

您有两个视图,所以(从零开始计算),您会想要在第1个位置添加;只需调用 ll.addView(image,1);即可将其放置在两个TextViews之间。


1
我想知道如果我有两个视图,我希望第一个视图占据布局的一半以上,而另一个视图只占据布局的小空间,我该怎么做?我可以在其他布局中像相对布局这样使用吗? - Lily
1
我来这里是想学习如何将一个View添加到相对于另一个子View的特定位置,以便动态确定。我意识到问题并没有直接问到那个,所以我不会自己回答,而是在这个答案上补充一下如何做到这一点:首先使用ViewGroup.indexOfChild(View view)获取其他View的索引,然后使用此处详细说明的索引的addView方法插入一个新的View相对于现有的一个。 - Tim M.
@TimM。老兄,非常感谢。我正好在找那个东西。 - Big_Chair

11

文档中指出您可以使用索引将其插入到所需位置。我看到您只使用了视图的签名,您是否尝试过带有索引参数的签名?

public void addView(View child, int index) 

我在将TextView插入布局时遇到了IndexOutOfBoundsException错误。它只解释了index=2,count=0... - nidhi

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());

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>

1
将一个ImageView添加到xml中,如果它没有被使用,就将其设置为不可见(image.setVisibility(View.INVISIBLE))。当没有设置图像时,它可能什么也不显示。

2
我认为你可能在把一些简单的东西复杂化了 :) - Jack
我认为你想使用View.GONE,除非你想留下一个空白的空间。你也可以使用索引插入。 - Idistic

0

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

                val targetPosition = oldLL.indexOfChild(viewToAdd)

在视图组中添加一个视图到指定位置:
                newLL.addView(viewToAdd,targetPosition)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接