android - 创建 ImageView 和 TextView 的 ViewGroup 并以编程方式将其显示在 FlowLayout 中 - Android

标签 android android-linearlayout android-relativelayout viewgroup flowlayout

我有一个 Flow 布局,显示 4 个行对象,但我需要显示这 4 个对象,并在每个对象下方都有一个 TextView 。但它显示 2 个图像和 2 个 TextView ,并跳转到另一行。我尝试创建一个 ViewGroup 来将它们显示在一起,但我遇到了同样的问题。也许如果我尝试在组内设置 textView 的位置它可以工作,但我不知道该怎么做。

我得到了什么: What I got:

我想要什么: What I want:

我的 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="#3c4052"
    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">

    <com.nex3z.flowlayout.FlowLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingRight="5dp"
        app:childSpacingForLastRow="align"
        android:paddingLeft="5dp"
        app:rowSpacing="8dp"
        android:id="@+id/likesContainer"
        >

    </com.nex3z.flowlayout.FlowLayout>

</ScrollView>

我的代码:

ImageView iconLike = new ImageView(Register30.this);
            TextView txtLike = new TextView(Register30.this);

            iconLike.setImageDrawable(getDrawable(R.drawable.x));
            txtLike.setText("Unable");


            countLikesAdd++;
            removeMessageOrShow();


            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(140,130);
            lp.setMargins(5,5,5,5);
            iconLike.setLayoutParams(lp);
            txtLike.setLayoutParams(lp);
            txtLike.setTextColor(Color.parseColor("#FFFFFF"));
            fieldLike.setText("");
            ViewGroup gp = new LinearLayout(Register30.this);
            gp.addView(iconLike);
            gp.addView(txtLike);
            likesContainer.addView(gp);

最佳答案

第一种方法:静态方式 我认为你需要的是 TextView 上的可绘制图像。可绘制图像可以插入到除中心之外的任何位置。创建所需尺寸的合适图像 asset studio并在 TextView 上使用此行。

android:drawableTop="@drawable/ic_fb_logo_blue_48dp"

下面的代码编写了一个简单的实现。

<android.support.v4.widget.NestedScrollView
                android:id="@+id/svLikes"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="60dp"
                android:layout_marginEnd="14dp"
                android:layout_marginStart="14dp"
                android:layout_marginTop="48dp"
                android:orientation="vertical">

                <com.nex3z.flowlayout.FlowLayout
                    android:id="@+id/likesContainer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:paddingTop="5dp"
                    app:childSpacingForLastRow="align"
                    app:rowSpacing="8dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableTop="@drawable/ic_fb_logo_blue_48dp"
                        android:gravity="center"
                        android:text="@string/facebook"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableTop="@drawable/ic_fb_logo_blue_48dp"
                        android:gravity="center"
                        android:text="@string/facebook"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableTop="@drawable/ic_fb_logo_blue_48dp"
                        android:gravity="center"
                        android:text="@string/facebook"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableTop="@drawable/ic_fb_logo_blue_48dp"
                        android:gravity="center"
                        android:text="@string/facebook"
                        android:textSize="16sp" />

                </com.nex3z.flowlayout.FlowLayout>

            </android.support.v4.widget.NestedScrollView>

输出图像如下。我使用 NestedScrollView 而不是 ScrollView。 enter image description here

第二种方法:动态 通过编程,可以使用网格布局生成多个 View 。我使用 recylerview 的实现如下

Activity 类内部:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerOne = (RecyclerView) findViewById(R.id.recyclerView);

    initRecyclerView();

    //update whenever necessary
    adapterOne.updateItems(myList);
}

private void initRecyclerView(){
    //Datum is model class
    int spanCount = 4; // 4 columns in grid
    int spacing = 8;
    boolean includeEdge = true;

    GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
    recyclerOne.setHasFixedSize(false);
    recyclerOne.setLayoutManager(gridLayoutManager);
    recyclerOne.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
    adapterOne = new RecylcerViewAdapter(new ArrayList<Datum>());
    recyclerOne.setAdapter(adapterOne);
}

GridSpacingItemDecoration 类是

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

private int spanCount;
private int spacing;
private boolean includeEdge;

public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
    this.spanCount = spanCount;
    this.spacing = spacing;
    this.includeEdge = includeEdge;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view); // item position
    int column = position % spanCount; // item column

    if (includeEdge) {
        outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
        outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

        if (position < spanCount) { // top edge
            outRect.top = spacing;
        }
        outRect.bottom = spacing; // item bottom
    } else {
        outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
        outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
        if (position >= spanCount) {
            outRect.top = spacing; // item top
        }
    }
}

}

在回收器 View 适配器类中,在绑定(bind) View 期间,使用带有可绘制图像的单个 TextView ,如第一种方法所示并更新数据。为了更新整个列表,您创建以下函数并从 Activity 类中调用它:

public void updateItems(List<Datum> data) {
    list.clear()
    list.addAll(data)
    notifyDataSetChanged();
}

其他方法 我知道还有另一种方法,它是通过膨胀项目并在已创建的线性布局(或任何其他容器布局)上添加膨胀 View 来完成的。

使用您喜欢的任何方法。享受编码的乐趣!

关于android - 创建 ImageView 和 TextView 的 ViewGroup 并以编程方式将其显示在 FlowLayout 中 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45291289/

相关文章:

android - 如何从 webview 获取 html 内容?

android - 如何以编程方式在 LinearLayout 上设置波纹效果?

Android:2相对布局分为半屏

java - 将字符串转换为公钥时获取公钥为空

android - 如何在 android 中以椭圆形或 mask 形状裁剪图像?

android - 如何更改自定义创建的工具栏中菜单的颜色?

android - 如何在Android中实现这种布局?

android - 将多个嵌套的 LineairLayout 转换为单个 RelativeLayout

android - 当 Activity 打开时,为什么焦点没有设置到第一个文本字段?

android - URI 未注册 Android studio 错误