Change Margin of a Layout using XML in Layer-List
Asked Answered
S

2

8

I tried several options but I can't find the best way to do this: I want to change the margin of a Linear Layout when button is clicked using XML. Here's my code for the selected button:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:bottom="-1dp">
        <shape android:shape="rectangle" >
            <corners
                android:topLeftRadius="5dp"
                android:topRightRadius="5dp" />

            <solid android:color="#424346" />

            <stroke
                android:width="1dp"
                android:color="#2d2e2f" />
        </shape>
    </item>
</layer-list>

Now I want to add the changing of margin which if put in a separate file would look like this:

<resources>
    <style android:id="@+id/tab_label" >
        <item android:layout_marginTop="7dp"/>
    </style>
</resources>

I tried adding it as an item to the layer list but it returns an error.

Putting it in another file is quite problematic because Android can't allow two styles to be used in one element. So I want to merge the two things in one XML file.

Or, what's the best way doing this?

Sisson answered 29/8, 2013 at 7:11 Comment(0)
S
25

Just solved it after roughly a week of trials. On this line of code:

<item android:bottom="-1dp">

Modify it to this:

<item android:bottom="-1dp" android:top="4dp">

You can customize the the number based on your preference.

Sisson answered 7/9, 2013 at 8:38 Comment(1)
Must have been an interesting weekNettle
M
0

i used this trick

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

    <stroke
        android:width="4dp"
        android:color="@android:color/transparent" />

    <solid android:color="#FDE0B4" />
    <corners android:radius="4dp" />

</shape>
Montfort answered 6/2, 2023 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.