如何动态地在ScrollView中的ImageView上添加按钮? Android系统。 Xamarin

时间:2016-04-17 10:43:16

标签: android android-layout xamarin imageview scrollview

我有一个axml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylinearlayout"
android:orientation="vertical"
android:configChanges="orientation|keyboardHidden"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
    android:id="@+id/surfaceView"
    android:layout_height="1dip"
    android:layout_width="1dip" />
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView1">
    <LinearLayout
        android:id="@+id/filesScrollerLayout"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</ScrollView>
<VideoView
    android:layout_width="1dip"
    android:layout_height="1dip"
    android:id="@+id/videoView1" />

我的应用程序会拍摄照片和视频并将其放入LinearLayout。

当我添加视频时,我会动态创建一个ImageView,将视频的第一帧放在此ImageView中并将其添加到LinearLayout中。

private void addBitmapToLayout(Bitmap bitmap)
{
    ImageView iv = new ImageView(this);
    iv.SetImageBitmap(bitmap);
    linearlayout.AddView(iv);
}

如何动态地在LinearLayuot中将此按钮放在此ImageView上以启动视频? Like this 谢谢。

1 个答案:

答案 0 :(得分:0)

不是在ImageView添加LinearLayout,而是添加自定义布局,您可以在其中ImageView以及Button位于顶部

定义列表中每个项目的布局:

<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<ImageView
  android:id="@+id/imageView"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<Button
  android:id="@+id/button"
  android:layout_centerInParent="true"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
</RelativeLayout>

您的功能现在变为:

private void addBitmapToLayout(Bitmap bitmap) {
  LayoutInflater inflater = LayoutInflater.from(context);
  RelativeLayout layout = inflater.inflate(R.layout.list_item, null, false);
  ImageView iv = (ImageView) layout.findViewById(R.id.imageView);
  iv.SetImageBitmap(bitmap);

  //Add click listener to your Button etc...
  Button button = (Button) layout.findViewById(R.id.button);

  linearlayout.AddView(layout);
}