Associated with the TextView database for Android TextView

I need to place the TextView so that its baseline is 20dp from the bottom of the container.

How can i achieve this?

A layout with a bottom margin or padding gives the same result.

I would like to make the text "sit" on the purple line. When I write “sit,” I mean that “wert” should touch the line, not “q ... y”.

The indent / edge is the size of the purple square:

enter image description here

+6
source share
2 answers

Your problem is not the deviation / margin that the parent refers to, I think this is your font, I recommend that you change fontFamily:"yourStyle" even in the worst case you need to redefine your own font style, which is explained here Custom fonts and XML layouts (Android) or Set a specific font in styles.xml

+1
source

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.

+1
source

Source: https://habr.com/ru/post/980822/


All Articles