In my app I wanted to apply padding to all my screen top level container from the theme.
example :
here is the layout file.
<?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>
here is the app theme.
<!-- 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>
I can apply padding using android:padding
to the parent container RelativeLayout. But I don't want like this as I need to specify the same in each layout file, I wanted to define that in theme - AppTheme.
Any solution will be appreciated.
why you need to code extra using style?
You can set padding directly inside res/values/dimens.xml,it would be much easier. and whenever you need to increase or decrease the values you just need to change at single place for every activity
eg in dimens.xml
<dimen name="my_activity_padding">5dp</dimen>
and then in activity layout:
android:padding="@dimen/activity_padding"
The theme is not the solution since it will add padding to all view hierarchy under your layout. (your RelativeLayout as well as the EditText.
Use @Karan solution
<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>
use android:paddingtop in your style
只需将此行添加到您的主要主题<item name="android:padding">50dp</item>
,这是在Manifest.xml中使用的android:theme="@style/AppTheme">
它会影响所有使用此布局的布局主题。
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.