简体   繁体   English

约束集不起作用

[英]Constraint Set dont work

my problem is, that I have a constraint set, which dont work. 我的问题是,我有一个约束集,它不起作用。 I have already tried a lot, but nothing worked... The button and the text are at the left side and the text is behind the button. 我已经尝试了很多,但没有任何效果......按钮和文本位于左侧,文本位于按钮后面。

Here is a screenshot from the result at the moment 这是目前结果的截图 可视代码结果的图片

I want that the button is at the right side of the grey box and the text at the left top. 我希望按钮位于灰色框的右侧,文本位于左上方。 Is this possible with only code? 这只能用代码吗?

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

Put this line: 把这一行:

set.applyTo(cLayout);

after the cButton has been added into cLayout : cButton添加到cLayout

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

set.applyTo(cLayout);

because before cButton has been added into cLayout , its ConstraintSet.PARENT_ID is undefined. 因为在将cButton添加到cLayout ,其ConstraintSet.PARENT_ID未定义。

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

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