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

时间:2018-07-06 13:25:42

标签: android kotlin

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

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

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

the true result

但是结果变成这样:

the false result

代码:

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>

3 个答案:

答案 0 :(得分:0)

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

<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();
    }

输出 enter image description here

答案 1 :(得分:0)

将(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>

答案 2 :(得分:0)

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

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

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)