If you still need it, I wrote my own method so as not to create a lot of custom views. This works for me with TextView :
public static void applyExistingBotMarginFromBaseline(View view) { final int baseline = view.getBaseline(); final int height = view.getHeight(); final ViewGroup.MarginLayoutParams marginLayoutParams; try { marginLayoutParams = ((ViewGroup.MarginLayoutParams) view.getLayoutParams()); } catch (ClassCastException e) { throw new IllegalArgumentException("Applying margins on a view with wrong layout params."); } final int baselineMarginValue = baseline + marginLayoutParams.bottomMargin; marginLayoutParams.bottomMargin = baselineMarginValue - height; view.setLayoutParams(marginLayoutParams); }
You can apply it when the view is already measured, like this:
final TextView title = (TextView) findViewById(R.id.title); title.post(new Runnable() { @Override public void run() { Utils.applyExistingBotMarginFromBaseline(title); } });
Alternatively, you can use the data binding structure and write your own BindingAdapter using a small, custom method to use it from xml.
source share