将主题中顶部的活动/片段布局容器的填充/边距设置为多少?

6

在我的应用程序中,我想从主题中对所有屏幕顶级容器应用填充。

例如:

这是布局文件。

<?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中定义它。
非常感谢任何解决方案。

创建一个带有填充的布局,并在任何需要的地方重复使用它。http://developer.android.com/training/improving-layouts/reusing-layouts.html - Vaisakh N
我正在询问关于可重用的布局,它涉及主题。 - Mahesh Ramchandra Bhatkande
4个回答

2

为什么需要在样式中编写额外的代码?

您可以直接在res/values/dimens.xml中设置填充,这将更加方便。每当您需要增加或减少数值时,只需在单个位置更改所有活动的数值即可。

例如,在dimens.xml中:

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

然后在活动布局中:

android:padding="@dimen/activity_padding"

在发布问题之前,我正在使用这个解决方案。将这些尺寸放入dimens.xml文件中是一个好习惯。 - Mahesh Ramchandra Bhatkande

1
主题并不是解决方案,因为它会向布局下的所有视图层次结构添加填充(包括您的RelativeLayout以及EditText)。
使用@Karan的解决方案。

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
这个不起作用,它会给窗口添加填充。我的问题是关于父容器的。 - Mahesh Ramchandra Bhatkande

0

只需在您的主题中添加此行 <item name="android:padding">50dp</item>,该主题在 Manifest.xml 中使用了 android:theme="@style/AppTheme">,它将影响所有使用此主题的布局。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接