2015-11-04 21 views
2

在我的應用程序中,我想將填充應用於主題中的所有屏幕頂級容器。將填充/邊距設置爲主題中的頂部活動/分段佈局容器?

示例:

這裏是佈局文件。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <EditText 
     android:id="@+id/edt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

這裏是應用程序的主題。

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:windowFrame">@drawable/window_frame</item> 
    </style> 

我可以使用android:padding應用填充到父容器RelativeLayout。但我不想這樣,因爲我需要在每個佈局文件中指定相同的內容,所以我想在主題--AppTheme中定義它。

任何解決方案,將不勝感激。

+0

創建填充佈局和重新使用它在任何你想要http://developer.android.com/training/improving-layouts/reusing- layouts.html –

+0

我在問關於可重用的佈局,關於主題 –

回答

0
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:paddingTop">25dp</item> 
    </style> 

採用Android:paddingtop在你的風格

+1

這不起作用,它向窗口添加填充,我的問題是關於父容器的, –

1

爲什麼你需要使用樣式編寫額外的?

您可以直接在res/values/dimens.xml中設置填充,它會容易得多。並且無論何時您需要增加或減少數值,您只需在每個活動的單個位置進行更改

例如 在dimens.xml

<dimen name="my_activity_padding">5dp</dimen> 

,然後在活動佈局:

android:padding="@dimen/activity_padding" 
+0

在發佈問題之前,我正在使用這個soln。很好把這些維度放在dimens.xml文件中。 –

0

只是這行添加到這是在的Manifest.xml android:theme="@style/AppTheme">使用您的主旋律<item name="android:padding">50dp</item>它會影響這是使用的所有佈局這個主題。

0

該主題不是解決方案,因爲它會將填充添加到佈局下的所有視圖層次結構中。 (你的RelativeLayout還有的EditText。

使用@Karan解決方案

相關問題