【问题标题】:How to draw a view over another by using Constraint Layout dynamically如何通过动态使用约束布局在另一个上绘制视图
【发布时间】:2017-09-18 12:18:52
【问题描述】:

我需要动态地实现这一点。 ImageView 顶部是 TextView.

这是这个布局的代码。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:srcCompat="@drawable/button_facebook_login_pressed"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="0.672"
        app:layout_constraintVertical_bias="0.498" />

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/imageView2"
        app:layout_constraintTop_toTopOf="@+id/imageView2"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="@+id/imageView2"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="@+id/imageView2" />
</android.support.constraint.ConstraintLayout>

我正在尝试动态执行此操作。这就是我所做的。

     ConstraintLayout layout = (ConstraintLayout)view.findViewById(R.id.ll_seasonFragment);

     ImageView imageView= new ImageView(getActivity().getApplicationContext());

     TextView textView= new TextView(getActivity().getApplicationContext());

     final float scale = getResources().getDisplayMetrics().density;

    ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);

            int margin = (int) (5 * scale + 0.5f);

layout.addView(textView);
layout.addView(imageView);

这是一个接一个地添加Views,我需要知道如何将它们绘制在另一个上。

编辑

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="551dp"
        android:id="@+id/ll_seasonFragment"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:orientation="vertical"
        >

    </LinearLayout>

    </android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 什么是ll_seasonFragment?你能发布那个 XML 吗?
  • @Cheticamp : 我已经放了那个代码,里面没有项目
  • 这有点清楚了。问题在于 LinearLayout 和您对它的使用。你需要 LinearLayout 还是想用 ConstraintLayout 做所有事情?您可以按照 Jyoti Sharma 的建议进行操作,并使用 RelativeLayout 而不是 LinearLayout。如果您不需要内部布局,最好只使用 ConstraintLayout 并且可以做到。

标签: android performance android-layout android-fragments android-constraintlayout


【解决方案1】:

如果您想通过代码复制布局,则不需要线性布局。只需创建一个带有空 ConstraintLayout 的布局,如下所示:

activity_main.xml

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这也可以通过代码创建。下面将在这个空白布局中添加一个居中的TextView

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout = findViewById(R.id.constraintLayout);

        TextView textView = new TextView(this);
        textView.setId(View.generateViewId());
        textView.setText("This is a centered TextView");
        // Add view to the layout. Without constraints, they will all flop into the top
        // left corner of the layout.
        layout.addView(textView);

        // ConstraintSet will control all the connections between the views in the ConstraintLayout
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(layout);
        constraintSet.connect(textView.getId(), ConstraintSet.START,
                              ConstraintSet.PARENT_ID, ConstraintSet.START);
        constraintSet.connect(textView.getId(), ConstraintSet.END,
                              ConstraintSet.PARENT_ID, ConstraintSet.END);
        constraintSet.connect(textView.getId(), ConstraintSet.TOP,
                              ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        constraintSet.connect(textView.getId(), ConstraintSet.BOTTOM,
                              ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        constraintSet.applyTo(layout);
    }
}

这里的关键思想是ConstraintSet,它将创建将视图放置在正确位置所需的约束。您可以以几乎相同的方式添加和限制您的 ImageView

我建议你坚持使用ConstraintSet.STARTConstraintSet.END 而不是ConstraintSet.LEFTConstraintSet.RIGHT。上次检查的时候左右都不行。

【讨论】:

    【解决方案2】:

    先在ConstraintLayout中添加相对布局,再在相对布局中添加imageView和textview。

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2017-02-11
      相关资源
      最近更新 更多