[英]Set the padding/margin to the top activity/fragment layout container in theme?
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. 我可以使用
android:padding
将android:padding
应用于父容器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. 但我不想这样,因为我需要在每个布局文件中指定相同的内容,我想在主题中定义它 - 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. 您可以直接在res / values / dimens.xml中设置填充,这将更容易。 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 例如在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.
(您的RelativeLayout以及EditText。
Use @Karan solution 使用@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>
use android:paddingtop in your style 在你的风格中使用android:paddingtop
只需将此行添加到您的主要主题<item name="android:padding">50dp</item>
,这是在Manifest.xml中使用的android:theme="@style/AppTheme">
它会影响所有使用此布局的布局主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.