【问题标题】:Android can't remove padding around custom title barAndroid 无法删除自定义标题栏周围的填充
【发布时间】:2013-03-19 05:08:00
【问题描述】:

我正在向标题标题添加一个自定义 img,但无论我做什么,我仍然在 img 的每一侧都有一个小间隙(也显示在这个 question 中)

这是我的 strings.xml 文件中的 xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello"></string>
    <string name="app_name"></string>
    <style name="LargeTitleTheme" parent="android:Theme.Light">
        <item name="android:windowTitleSize">44dip</item>
    </style>
</resources>

这是我活动中的代码(此时忽略草率 - 绝望的编码)

ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
    LinearLayout root = (LinearLayout) decorView.getChildAt(0);
    FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
    TextView title = (TextView) titleContainer.getChildAt(0);
    title.setGravity(Gravity.CENTER);
    Drawable drawable = getResources().getDrawable(R.drawable.nav);
    drawable.setBounds(0,0,0,0);
    title.setBackgroundDrawable(drawable);
    title.setPadding(0,0,0,0);
    title.setIncludeFontPadding(false);

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以通过应用自定义主题来覆盖默认填充,例如

    <style name="customTheme" parent="android:Theme">
            <item name="android:windowTitleBackgroundStyle">@style/WindowTitleStyle</item>
    </style>
    
     <style name="WindowTitleStyle">
        <item name="android:padding">0px</item>
    </style>
    

    这将从自定义标题栏中删除默认填充。

    【讨论】:

      【解决方案2】:

      原来我不得不修改标题容器本身的背景颜色:)

      private void setNavigationAndTitle() {
              setTitle("Random Title");
              ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
              LinearLayout root = (LinearLayout) decorView.getChildAt(0);
              FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
              titleContainer.setBackgroundColor(Color.BLUE);
              TextView title = (TextView) titleContainer.getChildAt(0);
              title.setTextSize(20);
              title.setGravity(Gravity.CENTER);
              title.setBackgroundColor(Color.BLUE);
      }
      

      【讨论】:

      • 该死的谢谢我实际上发现大多数关于 android 的互联网信息都非常糟糕
      • 只是要明确一点——这在 Android 平板电脑上不起作用(对 FrameLayout 的强制转换失败了)——我还没有弄清楚这 应该是什么如果/当我在平板电脑 android 设备上工作时,我会更新帖子
      【解决方案3】:

      从 4.2 和 ADT 21 开始,在创建默认布局时,标准尺寸会添加到父容器中:

      <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"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" >
      
      </RelativeLayout>
      

      值位于 res/dimens.xml 中:

      <!-- Default screen margins, per the Android Design guidelines. -->
      <dimen name="activity_horizontal_margin">16dp</dimen>
      <dimen name="activity_vertical_margin">16dp</dimen>
      
      </resources>
      

      要么将它们从父级中删除,要么将 dimens.xml 值更改为您想要的值。

      【讨论】:

        【解决方案4】:

        您可以尝试将 setPadding 值设为负数

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-08
          • 2013-01-07
          • 2017-11-23
          • 1970-01-01
          • 1970-01-01
          • 2013-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多