[英]Set the padding/margin to the top activity/fragment layout container in theme?
在我的應用程序中,我想從主題中應用填充到我的所有屏幕頂級容器。
例如:
這是布局文件。
<?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
將android:padding
應用於父容器RelativeLayout。 但我不想這樣,因為我需要在每個布局文件中指定相同的內容,我想在主題中定義它 - AppTheme。
任何解決方案將不勝感激。
為什么你需要使用樣式編寫額外代碼?
您可以直接在res / values / dimens.xml中設置填充,這將更容易。 無論何時需要增加或減少值,您只需在每個活動的單個位置進行更改
例如在dimens.xml中
<dimen name="my_activity_padding">5dp</dimen>
然后在活動布局中:
android:padding="@dimen/activity_padding"
主題不是解決方案,因為它將為您的布局下的所有視圖層次結構添加填充。 (您的RelativeLayout以及EditText。
使用@Karan解決方案
<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
只需將此行添加到您的主要主題<item name="android:padding">50dp</item>
,這是在Manifest.xml中使用的android:theme="@style/AppTheme">
它會影響所有使用此布局的布局主題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.