Android 无法删除自定义标题栏周围的填充

2024-02-16

我正在向标题标题添加一个自定义 img,但无论我做什么,img 的每一侧仍然有一个小间隙(也显示在这个中)question https://stackoverflow.com/questions/2665507/custom-title-bar-without-padding-android)

这是我的 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);

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

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

 <style name="WindowTitleStyle">
    <item name="android:padding">0px</item>
</style>

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 无法删除自定义标题栏周围的填充 的相关文章

随机推荐