简体   繁体   中英

How can I use the negative of an Android attribute in XML?

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?

After various attempts to find a solution to this the best way that I found was to simple add another icon after the text but to make it invisible:

<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"/>
    <ImageView
            style="@style/Test.NavBar.Heading.Icon"
            android:id="@+id/test_blank"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/test_title"
            android:layout_toEndOf="@+id/test_title"
            android:src="@drawable/ic_launcher"
            android:visibility="invisible"/>
</RelativeLayout>

With this additional but invisible icon the TextView is now centered in the middle of the RelativeLayout and this solves the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM