Padding Precedence in Xamarin.Android

Nathan Sokalski 4,111 Reputation points
2021-08-27T04:03:17.74+00:00

When creating my layout (*.xml) files for Xamarin.Android, one thing that constantly drives me crazy is when I need to override a padding property. I have a Style that includes the following:

<item name="android:padding">0px</item>

Based on what I could find at

https://developer.android.com/reference/android/view/View#attr_android:padding

The precedence is as follows:

Highest Precedence

paddingStart, paddingEnd
padding
paddingHorizontal, paddingVertical
paddingLeft, paddingTop, paddingRight, paddingBottom

Lowest Precedence

For example, if I want my padding on the right to be 5dp, I would think using my Style along with android:paddingEnd="5dp" would work, since paddingEnd is higher precedence, but that does not seem to work. Sure, I could just change my Style, but that kind of defeats the purpose of Styles. I would have thought that Left, Top, Right, and Bottom would be highest precedence so that you could individualize all Views, but regardless of what I think and what is true, is there a way to set the padding(s) and still use my Style? Thanks.

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-08-30T02:05:23.497+00:00

    Hi, @Nathan Sokalski Here is the test result, please see the screenshot.

       <GridLayout   
           xmlns:android="http://schemas.android.com/apk/res/android"  
           android:layout_width="wrap_content" android:layout_height="wrap_content"   
           android:columnCount="1"   
           android:rowCount="4">  
            <TextView   
               android:layout_width="match_parent"   
               android:layout_height="30dp"   
               style="@style/CustomTextViewStyle"   
               android:layout_column="0"   
               android:layout_row="0"   
               android:paddingStart="15px"  
               android:background="#f00"  
               android:gravity="center_vertical|right"   
               android:text="25"  
               />  
            <!--Other TextViews-->  
        </GridLayout>  
         
       <style name="CustomTextViewStyle">  
         <item name="android:padding">0px</item>  
       </style>  
    

    The debug test result:
    127451-image.png

    It seems you add tools:Text for these textViews. Did you check the function in xml preview? Please debug the project to check the test result. The result of the 'padding' seems not correct when adding both padding for a view in the style and the layout.xml.

    Here is the preview 'padding' result.
    127453-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.