【问题标题】:Set the padding/margin to the top activity/fragment layout container in theme?将填充/边距设置为主题中的顶部活动/片段布局容器?
【发布时间】:2015-11-04 06:04:48
【问题描述】:

在我的应用中,我想将填充应用到主题中的所有屏幕顶级容器。

例子:

这是布局文件。

<?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 中定义它。

我们将不胜感激任何解决方案。

【问题讨论】:

标签: android android-layout android-activity


【解决方案1】:

为什么需要使用样式进行额外编码?

您可以直接在 res/values/dimens.xml 中设置填充,这样会容易得多。并且每当您需要增加或减少值时,您只需要在每个活动的单个位置进行更改

例如 在dimens.xml中

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

然后在活动布局中:

android:padding="@dimen/activity_padding"

【讨论】:

  • 我在发布问题之前正在使用这个解决方案。最好将这些维度放在 dimens.xml 文件中。
【解决方案2】:

主题不是解决方案,因为它会为布局下的所有视图层次结构添加填充。 (您的 RelativeLayout 以及 EditText。

使用@Karan 解决方案

【讨论】:

    【解决方案3】:
     <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

    【讨论】:

    • 这不起作用。它为窗口添加了填充。我的问题是关于父容器的。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2016-11-27
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多