简体   繁体   English

ImageView填充影响了我在imageview下方的textview布局

[英]ImageView padding affecting my textview layout below the imageview

I'm trying to create something similar to what Facebook has done below: 我正在尝试创建类似于Facebook在以下方面所做的事情:

Facebook群组

I have created an empty RelativeLayout: 我创建了一个空的RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fav_cake_rl">
</RelativeLayout>

Then I wrote the following code to dynamically create my views. 然后,我编写了以下代码来动态创建视图。 As I would like 4 circles in one row going across the page (facebook only had 3), I obtained the DisplayMetrics which i placed into an object called "dm" in the code below and then divided the widthpixels by 4. 正如我希望在页面中连续四圈(facebook只有3个)一样,我获得了DisplayMetrics,并将其放入下面的代码中的“ dm”对象中,然后将widthpixels除以4。

The layouts are created dynamically in a recyclerview. 布局是在recyclerview中动态创建的。

    RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fav_cake_rl);

    CircularImageView circularImageView = new CircularImageView(context);
    RelativeLayout.LayoutParams circlellp = new RelativeLayout.LayoutParams(dm.widthPixels/4, dm.widthPixels/4);
    circularImageView.setLayoutParams(circlellp);
    circularImageView.setId(1);
    Drawable drawable = (Drawable) ContextCompat.getDrawable(context, R.drawable.cake);
    circularImageView.setImageDrawable(drawable);
    rl.addView(circularImageView);

    final TextView groupname = new TextView(context);
    RelativeLayout.LayoutParams textLp = new RelativeLayout.LayoutParams(dm.widthPixels/4, RelativeLayout.LayoutParams.WRAP_CONTENT);
    textLp.addRule(RelativeLayout.BELOW, 1);
    groupname.setLayoutParams(textLp);
    groupname.setGravity(Gravity.CENTER_HORIZONTAL);
    groupname.setText("StrawBerry Fields");
    rl.addView(groupname);

The end result looks like this: 最终结果如下所示:

草莓蛋糕

I don't really want the circles to be so big or so close together so then I added padding and margin to the imageview: 我真的不希望圆圈太大或太靠近,所以我在imageview中添加了padding和margin:

    CircularImageView circularImageView = new CircularImageView(context);
    RelativeLayout.LayoutParams circlellp = new RelativeLayout.LayoutParams(dm.widthPixels/4, dm.widthPixels/4);
    **circlellp.setMargins(margin, margin, 0, 0);**
    circularImageView.setLayoutParams(circlellp);
    **circularImageView.setPadding(32, 32, 32, 0);**
    circularImageView.setId(1);
    Drawable drawable = (Drawable) ContextCompat.getDrawable(context, R.drawable.cake);
    circularImageView.setImageDrawable(drawable);
    rl.addView(circularImageView);

Now, it looks like this: 现在,它看起来像这样:

增加利润后的草草

I do not want the text to be so far away from the imageview and it appears that while the padding has reduce the imageview, it has also added an extra space between the imageview and the text. 我不希望文本离imageview太远,并且看起来虽然填充减少了imageview,但它还在imageview和文本之间添加了额外的空间。

How can I get the textview and the imageview to be close to one another? 我如何才能使textview和imageview彼此接近?

UPDATE: 更新:

I tried it with XML instead by doing the following: 我通过执行以下操作使用XML进行了尝试:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fav_group_rl">

    <com.example.simon.customshapes.CircularImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/circlecake"
        android:layout_centerHorizontal="true"
        android:padding="24dp"
        android:layout_margin="8dp"
        android:src="@drawable/cake"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@id/groupname"
        android:text="StrawBerry Fields"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/circlecake"/>

</RelativeLayout>

To get 4 of them to display properly, I tried this: 为了使其中4个可以正确显示,我尝试了以下操作:

        RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fav_group_rl);
        RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(dm.widthPixels/4, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rl.setLayoutParams(llp);

When I ran this app, nothing was displayed. 当我运行该应用程序时,什么都没有显示。

Why not use xml to create one item and repeat it in recyclerview using adapter ? 为什么不使用xml创建一个项目并使用适配器在recyclerview中重复它呢? Xml will give you better control while designing your view and you are using recyclerview anyways. Xml将在设计视图时为您提供更好的控制,并且无论如何您都在使用recyclerview。

Using XML, you can easily divide screen width into n equal segment using layout_weight trick. 使用XML,您可以使用layout_weight技巧轻松地将屏幕宽度划分为n相等的段。 As far as I know, you can only use this attribute in LinearLayout s. 据我所知,您只能在LinearLayout使用此属性。

However nothing will go wrong by LinearLayout , you can, of course, compose multiple layouts inside each other. 但是, LinearLayout不会出错,您当然可以在彼此内部组成多个布局。

Let's head back to your case. 让我们回到你的情况。 In your case you would need a horizontal LinearLayout and then divide it into 4 equal cells. 在您的情况下,您需要一个水平LinearLayout ,然后将其划分为4个相等的单元格。 This could be done by a XML code like this. 这可以通过这样的XML代码来完成。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <!-- Cell 1 -->

    </FrameLayout>
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <!-- Cell 2 -->

    </FrameLayout>
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <!-- Cell 3 -->

    </FrameLayout>
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <!-- Cell 4 -->

    </FrameLayout>

</LinearLayout>

Note: layout_width of those cells should be zero. 注意:这些单元格的layout_width应该为零。 And layout_weight is the weight by which the whole width is divided. layout_weight是整个宽度除以的权重。 This could be different for each cell to producing heterogeneous cells. 对于每个细胞而言,对于产生异质细胞而言可能是不同的。

More Info 更多信息

I found the solution to this by actually playing around with margins in the end. 我最终通过实际处理边距找到了解决方案。

For the imageView, to reduce the size of the imageview, I had set the margin to some 8dp for top, left and right but not bottom. 对于imageView,为了减小imageview的大小,我将顶部,左侧和右侧的边距设置为8dp,但未设置底部。

Then for the textview, I have set the margin to -8dp for the top to compensate for the margin taken by the imageView at the imageView's top. 然后对于textview,我将顶部的边距设置为-8dp,以补偿imageView顶部的imageView所留的边距。

Then to draw the actual linearLayout properly, I had to set the linearLayout at the height of the imageView (dm.widthPixels/4), add on the margin at the top of the imageView and then the height of the textview (h). 然后,要正确绘制实际的linearLayout,我必须将linearLayout设置为imageView的高度(dm.widthPixels / 4),在imageView的顶部添加边距,然后添加textview的高度(h)。

    RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fav_group_rl);

    CircularImageView circularImageView = new CircularImageView(context);
    RelativeLayout.LayoutParams circlellp = new RelativeLayout.LayoutParams(dm.widthPixels/4, dm.widthPixels/4);
    circlellp.setMargins(margin, margin, margin, 0);
    circularImageView.setLayoutParams(circlellp);
    circularImageView.setId(1);
    Drawable drawable = (Drawable) ContextCompat.getDrawable(context, R.drawable.cake);
    circularImageView.setImageDrawable(drawable);
    rl.addView(circularImageView);

    final TextView groupname = new TextView(context);
    RelativeLayout.LayoutParams textLp = new RelativeLayout.LayoutParams(dm.widthPixels/4, RelativeLayout.LayoutParams.WRAP_CONTENT);
    textLp.addRule(RelativeLayout.BELOW, 1);
    groupname.setLayoutParams(textLp);
    textLp.setMargins(0,-margin,0,0);
    groupname.setGravity(Gravity.CENTER_HORIZONTAL);
    groupname.setText("StrawBerry Fields");
    rl.addView(groupname);
    groupname.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < 16) {
                h = groupname.getHeight();
                groupname.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                h = groupname.getHeight();
                Log.e("groupnameH: ", ""+h);
                groupname.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    });

    RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(dm.widthPixels/4, dm.widthPixels/3+ h + margin);
    rl.setLayoutParams(llp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM