2013-07-30 28 views
2

我有一個包含LinearLayout,它有一個與它相關的樣式,可以給容器20dp的填充。這非常適合99%的內部元素。可能有一個元素我想要在屏幕上延伸(android:layout_width =「match_parent」),並忽略由包含LinearLayout設置的填充。這可能嗎?或者,我是否需要從包含的LinearLayout中移除樣式並將其單獨應用於每個其他元素?Android中的子元素可以忽略其包含的樣式嗎?

(某些屬性排除簡潔)

<LinearLayout 
    android:padding="20dp"> 

    <TextView 
     android:id="@+id/txtTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/txtDate" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <!-- Would like this to ignore padding, extend fully. --> 
    <TextView 
     android:id="@+id/txtExperienceTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@style/sub_header_title" /> 

</LinearLayout> 

回答

3

是的,你真的不能挑選這樣的。我會從LinearLayout中移除填充並將margin應用於孩子。

要擴大loeschg在下面的評論中所說的話,我建議使用「兒童風格」。由於看起來每個孩子只需要左/右邊距,因此可以節省一些打字的時間,並且看起來更乾淨,只需在每個孩子身上劃一條線,而不是分別在左邊和右邊分別設置。

+1

我不知道的方式做到這一點,無論是。我認爲@Geobits說的是正確的。如果你還沒有在xml中使用該值,那麼我建議創建一個維持特定維度值(類似於Strings.xml)的維度文件。它至少可以讓你遵循一些DRY原則。 – loeschg

0

您不能接觸到容器。如果您有異常,請將style應用於元素而不是容器。

-1

是的它會忽略它,你寫的佈局是這樣的。

總體思路

int x = 7; 
x = 20; 

Show me that x = 7; ---You wanted to do something like this whit your layout 

<TextView 
    android:id="@+id/txtTitle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/txtDate" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<!-- Would like this to ignore padding, extend fully. --> 
<TextView 
    android:id="@+id/txtExperienceTitle" 
    style="@style/sub_header_title" // --- Style before adding laout_width if you want the layout to be match_parent 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 

相關問題