我有一个的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);
不过,我想补充的2个TextViews之间的ImageView在我的LinearLayout。我似乎无法找到在Android文档的方式来添加一个视图之前另一种观点,或之后。我怎样才能做到这一点?
注:我通话
setContentView(R.layout.main);
在我添加的ImageView到的LinearLayout。
当添加一个View
到ViewGroup
,可以specify an index其设定在父视图的位置。
你有两个视图等等(从零算起)你想在第一的位置添加;只需拨打ll.addView(image, 1);
把它放置在两个TextViews
之间。
该文档说明你可以使用索引,将其插入,你想要的。我看到您只使用视图的签名,你尝试与索引参数签名?
public void addView(View child, int index)
我面临着类似的问题。在我来说,我想在另一个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());
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>
添加的ImageView到XML,如果它不被使用,使无形的(image.setVisibility(View.INVISIBLE))
。当没有图像设置可能不反正显示任何东西。
为了得到一个视图组的视图位置
val targetPosition = oldLL.indexOfChild(viewToAdd)
在一个视图组的位置处添加视图
newLL.addView(viewToAdd,targetPosition)