繁体   English   中英

如何以编程方式在相对布局中对齐CenterVertical

[英]How to align CenterVertical in Relative layout programmatically

我有一个像这样的线性布局:

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

以下函数获取位图,将其设置为以编程方式创建的图像视图,然后创建垃圾桶图标,并将整个对象放入相对布局中,最后将其放入线性布局中。 但是有一个问题,我希望结果是这样的:

真实结果

但结果变成这样:

错误的结果

码:

fun setImage(bitmap: Bitmap){

    try {
        val params = RelativeLayout.LayoutParams(375, 375)
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP)
        params.setMargins(9,0,9,0)

        val imageViewShowPic = ImageView(this)
        imageViewShowPic.setLayoutParams(params)
        imageViewShowPic.id = View.generateViewId()
        imageViewShowPic.requestLayout()

        val params2 = RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT)


        val relativeLayout = RelativeLayout(this)
        relativeLayout.setLayoutParams(params2)
        relativeLayout.requestLayout()

        val params3 = RelativeLayout.LayoutParams(75,75)
        params3.addRule(RelativeLayout.BELOW,imageViewShowPic.id)
        params3.addRule(RelativeLayout.CENTER_VERTICAL)

        val imageButtonDelete = ImageButton(this)
        imageButtonDelete.setLayoutParams(params3)
        imageButtonDelete.requestLayout()

        relativeLayout.addView(imageViewShowPic)
        relativeLayout.addView(imageButtonDelete)

        linearLayoutImages.addView(relativeLayout)

        imageViewShowPic.setImageBitmap(bitmap)
        imageButtonDelete.setImageResource(R.drawable.ic_delete)

        imageButtonDelete.setOnClickListener {
            linearLayoutImages.removeView(relativeLayout)
        }
    }
    catch (ex:Exception)
    {
        Toast.makeText(this,ex.message,Toast.LENGTH_SHORT).show()
    }
}

更新资料

线性布局本身会在水平滚动视图中(如果有区别的话)。

<HorizontalScrollView
                android:id="@+id/horizontalScrollViewImages"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:padding="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/buttonAddPic">

                <LinearLayout
                android:id="@+id/linearLayoutImages"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="1"></LinearLayout>
 </HorizontalScrollView>

使用以下代码,您可以实现所需的输出。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1"></LinearLayout>

在您的活动中

    try {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(375, 375);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.setMargins(9, 0, 9, 0);

        ImageView imageViewShowPic = new ImageView(this);
        imageViewShowPic.setLayoutParams(params);
        imageViewShowPic.setId(View.generateViewId());
        imageViewShowPic.requestLayout();

        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setLayoutParams(params2);
        relativeLayout.requestLayout();

        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(75, 75);
        params3.addRule(RelativeLayout.BELOW, imageViewShowPic.getId());
        params3.topMargin = 30;
        params3.addRule(RelativeLayout.CENTER_HORIZONTAL);

        ImageButton imageButtonDelete = new ImageButton(this);
        imageButtonDelete.setLayoutParams(params3);
        imageButtonDelete.requestLayout();

        relativeLayout.addView(imageViewShowPic);
        relativeLayout.addView(imageButtonDelete);

        linearLayoutImages.addView(relativeLayout);

        imageViewShowPic.setImageResource(R.drawable.ic_launcher_background);
        imageButtonDelete.setImageResource(R.drawable.ic_launcher_background);

    } catch (Exception ex) {
        Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
    }

输出值 在此处输入图片说明

将(RelativeLayout.CENTER_VERTICAL)设置为(RelativeLayout.CENTER)

val params3 = RelativeLayout.LayoutParams(75,75)
params3.addRule(RelativeLayout.BELOW,imageViewShowPic.id)
params3.addRule(RelativeLayout.CENTER)

android:layout_width =“ wrap_content”到android:layout_width =“ match_parent”

<LinearLayout
                    android:id="@+id/linearLayoutImages"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="1"></LinearLayout>

(代表问题作者发布解决方案)

我终于找到了解决方案! 我将相对布局更改为垂直线性布局(不知道是否重要):

val params2 = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)

    val linearLayout = LinearLayout(this)
    linearLayout.setLayoutParams(params2)
    linearLayout.orientation = LinearLayout.VERTICAL
    linearLayout.requestLayout()

然后设置imageButtonDelete的宽度以匹配父级。

val params3 = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)


    val imageButtonDelete = ImageButton(this)
    imageButtonDelete.setLayoutParams(params3)
    imageButtonDelete.requestLayout()
    imageButtonDelete.setBackgroundColor(Color.TRANSPARENT)

暂无
暂无

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

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