自定义标题栏中的问题(android)

时间:2012-05-11 12:47:12

标签: java android

当我通过添加图像在视图中自定义标题栏时,我面临一个问题,因为图像没有准确地固定到边缘。我已经附加了图像,您可以在其中看到窗口边距和添加的图像之间的空间。如何克服这个问题。下面是我用来自定义标题栏的代码。任何人都可以帮我解决这个问题。

java代码:

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
 setContentView(R.layout.home_view);
 this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.upwindow);

布局代码:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  android:orientation="vertical" 
   android:layout_width="fill_parent"
     android:layout_height="32dp">

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:background="@drawable/up_bar"
         android:src="@drawable/up_bar" />

<TextView 
    android:id="@+id/pagename"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="Sify MyStorage"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

enter image description here

4 个答案:

答案 0 :(得分:1)

对于不同的分辨率,您有不同的图像吗?可能是您的图像不适合该空间,并且您需要更大的图像。但请注意,您可能会遇到使用缩放功能在不同平台上工作的问题。

答案 1 :(得分:1)

在values文件夹中创建样式xml:

<resources>
  <style name="CustomWindowTitleBackground" /> 
  <style name="CustomTheme" parent="android:Theme">
  <item name="android:windowTitleSize">32dp</item> 
 <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground
 </item> 

</style>
</resources>

然后在清单中将其引用为:

 android:theme="@style/CustomTheme">

这将解决它。

希望这有帮助。

答案 2 :(得分:1)

您需要使用样式进行一些操作才能覆盖此行为。定义自定义主题(样式),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTitle">
        <item name="android:background">@drawable/up_bar</item>
    </style>
    <style name="myTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/customTitle</item>
    </style>
</resources>

然后在{em> AndroidManifest.xml

中将android:theme="@style/myTheme"应用于所需的活动标记,从而在您的活动中使用此主题

答案 3 :(得分:1)

requestWindowFeature(Window.FEATURE_NO_TITLE);

和setBackground到布局文件中的布局......

像,,,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  android:orientation="vertical"  
   android:layout_width="fill_parent"  android:background="@drawable/up_bar" <---Add this
     android:layout_height="32dp">

Remove imageview from xml