【问题标题】:Constraint Set dont work约束集不起作用
【发布时间】:2018-11-30 09:01:21
【问题描述】:

我的问题是,我有一个约束集,它不起作用。我已经尝试了很多,但没有任何效果...按钮和文字在左侧,文字在按钮后面。

这是目前结果的截图

我希望按钮位于灰色框的右侧,文本位于左上方。只用代码可以吗?

package com.example.sbt_local.ticketui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class MainActivity extends Activity {

    private LinearLayout lLayout;
    private ConstraintLayout cLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context context = this;

        lLayout = new LinearLayout(context.getApplicationContext());
        lLayout.setOrientation(LinearLayout.VERTICAL);

        cLayout = new ConstraintLayout(context.getApplicationContext());
        cLayout.setBackgroundColor(Color.parseColor("#FFEDEDED"));
        cLayout.setPadding(4,4,4,4);
        cLayout.setId(View.generateViewId());
        ConstraintSet set = new ConstraintSet();
        set.clone(cLayout);

        Button cButton = new Button(context.getApplicationContext());
        cButton.setText(">");
        cButton.setLayoutParams(new RelativeLayout.LayoutParams(135, ViewGroup.LayoutParams.MATCH_PARENT));
        cButton.setId(View.generateViewId());
        set.connect(cButton.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
        set.constrainHeight(cButton.getId(), 200);
        set.applyTo(cLayout);

        TextView tv_startDate = new TextView(context.getApplicationContext());
        DateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy");
        tv_startDate.setText("Test");
        tv_startDate.setId(View.generateViewId());

        // Constraint Set
        System.out.println("Layout ID: " + cLayout.getId());
        System.out.println("Button ID: " + cButton.getId());

        // Add Elements to Layout
        cLayout.addView(cButton);
        cLayout.addView(tv_startDate);

        // Add Ticket to Layout List
        TextView spacer = new TextView(context.getApplicationContext());
        spacer.setTextSize(4); // Spacer Size
        lLayout.addView(cLayout);
        lLayout.addView(spacer);
        LinearLayout llLayout = (LinearLayout) findViewById(R.id.layout);
        llLayout.addView(lLayout);
    }
}

【问题讨论】:

  • 定义不工作
  • 我在问题的顶部区域提供了一张图片。我写了我想要的和不工作的。你想从我这里知道什么? ;)

标签: java android constraints android-constraintlayout


【解决方案1】:

把这一行:

set.applyTo(cLayout);

cButton 被添加到cLayout 之后:

cLayout.addView(cButton);
cLayout.addView(tv_startDate);

set.applyTo(cLayout);

因为在cButton 被添加到cLayout 之前,它的ConstraintSet.PARENT_ID 是未定义的。

【讨论】:

  • set.clone(cLayout); 可以在进行更改之前添加,因为在我的情况下,视图消失了。使用 `TransitionManager.beginDelayedTransition(cLayout);` 会减慢进程,因此您可以进一步分析。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 2015-03-13
  • 2017-08-24
  • 2014-11-20
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
相关资源
最近更新 更多