簡體   English   中英

以編程方式在LinearLayout的下一行中添加圖像-Android

[英]Add Image in next line in LinearLayout Programatcally - Android

我有一個LinearLayout,它的方向是水平的,但是當它在一行中與4個元素交叉時,它會不斷添加水平,我希望它自動更改為下一行,例如:

怎么了: 什么時候發生:

我想要的是: 我想要的是:

我的代碼:

btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            countLikesAdd++;
            removeMessageOrShow();
            ImageView iconLike = new ImageView(Register30.this);
            iconLike.setImageDrawable(getDrawable(R.drawable.musicicon));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(140,130);
            lp.setMargins(5,5,5,5);
            iconLike.setLayoutParams(lp);
            relativeLayout.addView(iconLike);
        }
    });

我的XML:

<ScrollView
    android:id="@+id/svLikes"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="60dp"
    android:layout_marginEnd="14dp"
    android:layout_marginStart="14dp"
    android:layout_marginTop="48dp"
    android:background="#2f3342"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/btnAdd"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1">
    <LinearLayout
        android:id="@+id/linearLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:paddingEnd="5dp"
        android:paddingTop="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        >
    </LinearLayout>
</ScrollView>

如果某些內容不合適, LinearLayout不支持換行的概念。

對於少數固定項目,最好的解決方案可能是使用Google搜索時可用的許多“ FlowLayout”庫之一。

對於大量項目(或在設計時不知道的項目),可以將RecyclerViewGridLayoutManager一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM