添加视图到的LinearLayout在指定位置 [英] Adding a view to a LinearLayout at a specified position
问题描述
我有一个LinearLayout中下面的main.xml文件
I have the following main.xml file with a 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>
我的图像通过code在运行时添加到LinearLayout中,像这样
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
不过,我想增加2 TextViews之间的ImageView在我的LinearLayout。我似乎无法找到一种方法在Android文档添加一个视图中的另一个视图之前,或之后。我怎样才能做到这一点?
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB 我称之为
setContentView(R.layout.main);
在我添加的ImageView到的LinearLayout。
Before I add the ImageView to the LinearLayout.
推荐答案
在添加查看
到的ViewGroup
,您可以<一href="http://developer.android.com/reference/android/view/ViewGroup.html#addView%28android.view.View,%20int%29">specify索引这台父视图的位置。
When adding a View
to a ViewGroup
, you can specify an index which sets the position of the view in the parent.
您有两种意见,因此(从零算起)你想添加的第一个位置;只需拨打 ll.addView(图像,1);
将它放置在二者之间 TextViews
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1);
to have it placed in between the two TextViews
.
这篇关于添加视图到的LinearLayout在指定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!