Home > database >  TextView margins not working inside LinearLayout
TextView margins not working inside LinearLayout

Time:02-27

I have this simple xml below. The problem is that none of the layout_marginLeft, layout_marginRight, layout_marginTop or layout_marginBottom work. But layout_margin works!

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp" <-- does not work
            android:layout_marginTop="16dp" <-- does not work
            android:text="This is a text!"
            />

</LinearLayout>

Also, I noticed that when I add an incomplete layout_margin="0" (without dp) it causes an error but the marginLeft, marginRight, etc. work! But then I have an error and it will not compile. What is going on here?!

CodePudding user response:

Found the culprit! Turns out I had this line in my themes.xml

<item name="android:layout_margin">2dp</item>

which was messing up all the layout_margin calls. Removed the line and now the margins work correctly.

  • Related