【问题标题】:Custom title bar issue自定义标题栏问题
【发布时间】:2021-01-17 14:49:08
【问题描述】:

我设置了一个自定义标题栏作为示例 http://coderzheaven.com/2011/06/custom-title-bar-in-android/ ,一切正常,但仍然从原始标题出现蓝框。我唯一添加的是将背景颜色设置为自定义标题为 (android:background="#EECBAD")。如何去除这个蓝框?

###custom_title.xml:

    <RelativeLayout 
     android:id="@+id/RelativeLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#EECBAD"
     xmlns:android="http://schemas.android.com/apk/res/android">
     <ImageView
     android:layout_width="40dip" 
     android:id="@+id/ImageView01" 
     android:background="@drawable/ic_launcher" 
     android:layout_height="40dip" /> 
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  
    android:id="@+id/tv" 
    android:text="ELNABIH"
    android:layout_toRightOf="@+id/ImageView01"
    android:textColor="@drawable/red"
    android:textStyle="bold" 
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip" />
    </RelativeLayout>

java.class

      package com.test.list;
    import android.app.Activity;
     import android.os.Bundle;
     import android.view.Window;
     import android.widget.TextView;
     public class Tttt extends Activity {  
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
           super.onCreate(savedInstanceState); 
           Boolean customTitleSupported = requestWindowFeature
           (Window.FEATURE_CUSTOM_TITLE); 
           setContentView(R.layout.main);  
           if (customTitleSupported) { 
               getWindow().setFeatureInt
           (Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
               TextView tv = (TextView) findViewById(R.id.tv);  
               tv.setText("name");  


             }
           }
         }

【问题讨论】:

  • 请在您修改背景的地方添加您的布局代码
  • 你能提供你的主布局文件的内容吗?

标签: android android-layout


【解决方案1】:

兄弟,我认为link 肯定会对您有所帮助。我认为您也必须在 xml 中定义样式和主题。

请在 res 下的 values 文件夹中创建 styles.xml & Themes.xml。编写如下代码:

styles.xml 的代码

    <?xml version="1.0" encoding="UTF-8"?>
    <resources> <style name="WindowTitleBackground" parent="android:Theme"> 
    <item   name="android:background">@android:color/background_dark</item>
    </style>
    </resources>

themes.xml 的代码

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <style name="MyTheme" parent="android:Theme">
    <item name="android:windowTitleSize">54px</item>
    <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>                 
    </style>
    </resources>  

并将其插入您的清单 - android:theme="@style/MyTheme"

【讨论】:

    【解决方案2】:

    尝试覆盖“android:windowTitleBackgroundStyle”。

    【讨论】:

      【解决方案3】:

      如果你为tv TextView设置背景色,那是因为它还有一些左右填充,除了设置为wrap_content

      android:layout_marginLeft="5dip"
      android:layout_marginRight="5dip"
      

      您可以删除这些行,或尝试为父级设置背景颜色,RelativeLayout01

      【讨论】:

      • 我已经将背景颜色设置为相对布局而不是 textview ,请在上面更新
      • 您是否也尝试删除 2 个边距属性?我确信样式解决方案可以正常工作,但在我看来,对于这种情况,它并不是真正必要的。
      【解决方案4】:

      穿上style.xml:

      <style name="CustomWindowTitleBackground">
              <item name="android:background">@color/LightFacebook</item>
          </style>
      

      在@color/LightFacebook 上使用你的自定义颜色。

      【讨论】:

        【解决方案5】:

        您应该先隐藏默认标题。您应该在 AndroidManifest.xml 中为活动声明或在 java 代码中设置窗口参数。

        <activity 
            android:name=".MainActivity" 
            android:theme="@android:style/Theme.Black.NoTitleBar" />
        

        【讨论】:

        • 请您查看上面的更新,我将其作为自定义布局而不是样式或主题,谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        • 2019-01-05
        • 2011-03-10
        相关资源
        最近更新 更多