/** * To get a helper for a view in relative layout * The view in the factory should HAS default layout param, usually inflated from xml * * @return helper to layout isInRelative layout */ public RelativeLayoutStick isInRelative() { return (RelativeLayoutStick) isInLayout(RelativeLayout.LayoutParams.class, true, 0, 0); }
/** * Choose the right builder stick according to layout params type * * @param layoutParamsType say {@link android.widget.LinearLayout.LayoutParams} * @param useExistingLayoutParams if true, no LayoutParams will be instantiated * @param width if not useExistingLayoutParams, new LayoutParams will use this as its width * @param height if not useExistingLayoutParams, new LayoutParams will use this as its height * @param <LP> the LayoutParam generic type * @return the corresponding builder stick */ private <LP extends ViewGroup.LayoutParams> Chopstick isInLayout(Class<LP> layoutParamsType, boolean useExistingLayoutParams, int width, int height){ LP lp = useExistingLayoutParams ? useExistingLayoutParams(layoutParamsType) : useNewLayoutParams(layoutParamsType, width, height); if (RelativeLayout.LayoutParams.class.equals(layoutParamsType)) { return new RelativeLayoutStick((RelativeLayout.LayoutParams) lp, mView); } else if(LinearLayout.LayoutParams.class.equals(layoutParamsType)){ return new LinearLayoutStick((LinearLayout.LayoutParams)lp, mView); } throw new UnsupportedOperationException("This Layout is not currently supported. A PR is welcome!"); }
public static ChopsticksFactory chop(@NonNull View view){ return new ChopsticksFactory(view); }
chop(findViewById(R.id.top_right)).isInRelative() .alignParentTop() .alignParentRight() .stick(); chop(findViewById(R.id.center)).isInRelative() .centerInParent() .stick(); chop(findViewById(R.id.below_center_align_left)).isInRelative() .below(R.id.center) .alignLeft(R.id.center) chop(findViewById(R.id.center_left_align_top)).isInRelative() .toLeftOf(R.id.center) .alignTop(R.id.center) textView.setText("BOTTOM CENTER HORIZONTAL"); textView.setId(R.id.bottom_center_horizontal); chop(textView).isInRelative(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) .alignParentBottom() .centerHorizontal()
/** * To get a helper for a view in relative layout * The view in the factory should HAS NO default layout param, usually instantiated programmatically * * @param width mostly the {@link ViewGroup.LayoutParams#WRAP_CONTENT} or {@link ViewGroup.LayoutParams#MATCH_PARENT} * @param height mostly the {@link ViewGroup.LayoutParams#WRAP_CONTENT} or {@link ViewGroup.LayoutParams#MATCH_PARENT} * @return helper to layout isInRelative layout */ public RelativeLayoutStick isInRelative(int width, int height){ return (RelativeLayoutStick) isInLayout(RelativeLayout.LayoutParams.class, false, width, height); }
public LinearLayoutStick isInLinear(){ return (LinearLayoutStick) isInLayout(LinearLayout.LayoutParams.class, true, 0, 0); }
public LinearLayoutStick isInLinear(int width, int height){ return (LinearLayoutStick) isInLayout(LinearLayout.LayoutParams.class, false, width, height); }