将图像视图动态添加到水平滚动(通过代码)

Adding Image views to the horizontal scroll dynamically (through code)

我刚开始研究 android 一天前,我正在研究卷轴。我已经做了一个,但我现在想动态地做同样的事情。

这是我的 activity_main.xml

的代码
<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal">

    <LinearLayout
        android:id= "@+id/linearlayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:onClick="onTouch">


    </LinearLayout>

</HorizontalScrollView>




<LinearLayout
    android:id="@+id/bottomlinear"
    android:layout_width="match_parent"
    android:layout_height="400px"
    android:gravity="center"
    android:background="#00ffff"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="46dp">>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/drop"
        android:textSize="30sp"
        android:text="Drop Zone" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Total"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Success"
        android:textSize="20sp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Fail"
        android:textSize="20sp" />

</LinearLayout>

基本上我想将可绘制对象中的 10 张图像动态添加到水平滚动中作为图像视图。非常感谢任何帮助或想法。

我认为你正在尝试做这样的事情:

mHScrollContentView = (ViewGroup) findViewbyId(R.id.linearlayout1);

ImageView iv1 = new ImageView(this);
iv.setImageResource(R.drawable.image_1);

ImageView iv2 = new ImageView(this);
iv.setImageResource(R.drawable.image_2);

ImageView iv3 = new ImageView(this);
iv.setImageResource(R.drawable.image_3);

ImageView iv4 = new ImageView(this);
iv.setImageResource(R.drawable.image_4);

mHScrollContentView.addView(iv1);
mHScrollContentView.addView(iv2);
mHScrollContentView.addView(iv3);
mHScrollContentView.addView(iv4);

你可以这样做。

  1. 创建一个包含 imageView 的 xml 布局。

image_item.xml

 <ImageView

            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:visibility="visible"
            android:adjustViewBounds="true"
        />
  1. 现在找到要放入java文件的容器,像这样。

    LinearLayout containerLayout = (LinearLayout)findViewById(R.id.linearlayout1);
    
  2. 现在只需 运行 循环直到 10 并在 运行 时间添加视图。

         for(int a = 0 ; a < 10 ; a++)
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View inflatedView = inflater.inflate(R.layout.image_item, null);
            containerLayout.addView(inflatedView);
        }
    

希望对您有所帮助,如有问题欢迎讨论。

快乐编码:-)

http://sandyandroidtutorials.blogspot.in/2013/06/horizontal-listview-tutorial.html

你可能会在这里得到答案!

我解决了这个问题。我要 post 解决方案,希望它能帮助遇到类似问题的其他人。我最初制作了 3 个水平滚动视图,其中一个的 xml 是这样的

    <HorizontalScrollView
    android:id="@+id/HorizontalScrollView1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginBottom="1dp"
    android:background="#FFF"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/imgLayout1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">

    </LinearLayout>


</HorizontalScrollView>

我编写的用于在水平滚动视图的线性布局内创建图像视图的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   for (int j=1; j<=10; j++)
    {
        b1=j;
        create_img1("drawable/a"+j, b1);
    }


}

void create_img1(String ss, int ID)
{
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200, 200);
    parms.gravity = Gravity.CENTER;
    parms.setMargins(20, 20, 20, 20);
    final ImageView imageView = new ImageView(this);
    imageView.setLayoutParams(parms);

    int id = getResources().getIdentifier(ss, "id", getPackageName());
    imageView.setImageResource(id);
    linearLayout.addView(imageView);
    imageView.setId(ID);
}

我为具有拖放功能的多个滚动视图制作了一个,但我将其过滤掉了,如果您想在滚动视图中动态创建图像视图,这就是您要找的。希望这对遇到类似问题的其他人有所帮助。