I have a simple relative layout containing an ImageView for an icon and a TextView for a title:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Test.NavBar.Heading"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<ImageView
style="@style/Test.NavBar.Heading.Icon"
android:id="@+id/test_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
style="@style/Test.NavBar.Heading.Text.Large"
android:id="@+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/test_logo"
android:layout_toEndOf="@+id/test_logo"
android:layout_alignTop="@id/test_logo"
android:layout_alignBottom="@id/test_logo"
android:gravity="center_vertical"
android:text="My Title"/>
</RelativeLayout>
This is embedded in another view, which centres my view horizontally. All well and good except that I need the layout to be centred on the TextView rather than that RelativeLayout.
The only thing I have access to is an attribute which gives me the width of the item in the ImageView, called iconWidth. Normally you can offset the layout using a margin attribute, for example above I could add
android:layout_marginLeft="?attr/iconWidth"
but that pushes the layout to the right not the left. Instead I need to do something like:
android:layout_marginLeft="-?attr/iconWidth"
except of course that isn't valid syntax.
I have to do this in a single layout and have to do it in XML and can't ask for additional attributes to be added. Given these constraints, how can I offset the contents of the RelativeLayout as required?
0 comments:
Post a Comment