Skip to content

Instantly share code, notes, and snippets.

@jack24254029
Last active January 31, 2018 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jack24254029/8fca3c9080d207bc0290f66a1600aa72 to your computer and use it in GitHub Desktop.
Save jack24254029/8fca3c9080d207bc0290f66a1600aa72 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
private static final int ROOT_VIEW = 650;
private static final int CUSTOM_VIEW = 823;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConstraintLayout constraintLayout = new ConstraintLayout(this);
constraintLayout.setId(ROOT_VIEW);
addContentView(constraintLayout,
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextLayoutBuilder builder = new TextLayoutBuilder()
.setTextSize(50)
.setAlignment(Layout.Alignment.ALIGN_CENTER)
.setText("TextLayoutBuilder makes life easy");
Layout layout = builder.build();
CustomView customView = new CustomView(this);
customView.setId(CUSTOM_VIEW);
customView.setLayout(layout);
customView.setBackgroundColor(Color.YELLOW);
constraintLayout.addView(customView);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.constrainWidth(CUSTOM_VIEW, ConstraintSet.MATCH_CONSTRAINT);
constraintSet.constrainHeight(CUSTOM_VIEW, LayoutMeasureUtil.getHeight(layout));
constraintSet.connect(CUSTOM_VIEW, ConstraintSet.TOP,
ROOT_VIEW, ConstraintSet.TOP);
constraintSet.connect(CUSTOM_VIEW, ConstraintSet.START,
ROOT_VIEW, ConstraintSet.START);
constraintSet.connect(CUSTOM_VIEW, ConstraintSet.END,
ROOT_VIEW, ConstraintSet.END);
constraintSet.connect(CUSTOM_VIEW, ConstraintSet.BOTTOM,
ROOT_VIEW, ConstraintSet.BOTTOM);
constraintSet.applyTo(constraintLayout);
}
public class CustomView extends View {
private Layout layout;
public CustomView(Context context) {
super(context);
}
public void setLayout(Layout layout) {
this.layout = layout;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw the layout.
layout.draw(canvas);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment