2014-10-07 31 views
2

我有包含對應圖標的ImageView和用於標題的TextView一個簡單的相對佈局:如何在XML中使用Android屬性的否定?

<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> 

這嵌入到另一個視圖,其水平中心我的視圖。除了我需要將佈局集中在TextView而非RelativeLayout之外,所有這一切都很好。

我唯一可以訪問的是一個屬性,它給了我在ImageView中的項目寬度,稱爲iconWidth。通常情況下,你可以使用保證金的屬性,例如偏移的佈局上面我可以添加

android:layout_marginLeft="?attr/iconWidth" 

但推佈局到右邊而不是左邊。相反,我需要做這樣的事情:

android:layout_marginLeft="-?attr/iconWidth" 

當然,除非是無效的語法。

我必須在單個佈局中執行此操作,並且必須在XML中執行此操作,並且無法要求添加其他屬性。鑑於這些限制,我如何根據需要抵消RelativeLayout的內容?

+0

你不行。這些屬性被解析。可能用'-'解析將失敗 – Blackbelt 2014-10-07 13:24:34

+0

也許你可以通過代碼修復它:獲取當前的邊距並將其乘以-1,然後重新將其設置爲新的值。 – 2014-10-07 13:27:31

+0

我可能不明白這個問題,但是不能使用'android:layout_marginRight'將它左移嗎? – 2014-10-07 13:27:31

回答

0

各種嘗試找到解決這個,我發現是簡單的添加另一個圖標文本之後,但使不可見的最好辦法後:

<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> 

有了這個額外的,但無形的圖標TextView是現在集中在RelativeLayout的中間,這就解決了這個問題。

相關問題