【问题标题】:Custom ActionBar with a background image带有背景图像的自定义 ActionBar
【发布时间】:2015-01-21 17:06:42
【问题描述】:

我一直在尝试制作一个 ActionBar。起初我决定将其绘制为图像(下图中的第二个 actionBar),但注意到在某些设备上 Image 不会占据整个屏幕宽度。至于原来的 ActionBar,我在我的主 Activity onCreate 方法中使用了这段代码:

final ActionBar actionBar = getActionBar();
@SuppressWarnings("deprecation")
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.drawable.actionbar)); 
actionBar.setBackgroundDrawable(background);
//actionBar.setIcon(android.R.color.transparent);
actionBar.setDisplayShowTitleEnabled(false);

第一个 ActionBar 有一个浅色主题,图像被拉伸。我怎样才能让它变得透明,有没有办法设置 ActionBar 的高度,这样它就不会看起来那么拉伸了?我也试过用样式来做这个,结果是一样的。

编辑我遵循了 vinitius 的回答,所以我现在有了这个:result 图片是 482x104,我的手机是 480x800。这与将其用作纯图像时发生的问题相同。图片在 drawable-hdpi 文件夹中。

EDIT 2 通过将 android:scaleType="fitXY" 添加到 ImageView 中,它将使整个图像拉伸。

actionbar_layout.xml

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

<View
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#3383A8" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:src="@drawable/actionbar" />

</RelativeLayout>

EDIT 3 如果我将图像添加为RelativeLayout 背景,则其填充不适合较大设备上的整个宽度。如果我将它用作更大宽度设备上的图像,它将被拉伸。 Image

【问题讨论】:

  • 似乎在我添加“background.setTileModeX(android.graphics.Shader.TileMode.CLAMP);”时发生了变化现在我只需要一种方法来删除 ActionBar 背景中的浅色主题并更改高度,因为图像不适合..
  • 那么,它对你有用吗?
  • 它适用于较小的设备,但不适用于较大的设备。我应该回到使用普通的 ActionBar,因为图像被拉伸了。
  • 但是您的图像尺寸是否正确(mdpi、hdpi、xhdpi....)?

标签: android android-actionbar


【解决方案1】:

您可以设计自己的actionBar 视图,甚至可以从任何 xml 中对其进行扩充,然后:

    getActionBar().setCustomView(yourView);
    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

要调整栏的高度,您可以这样做:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">

   <!-- the height you desire according to your dimen -->

    <item name="android:height">@dimen/action_bar_height</item>
</style>

然后,应用您的主题。它对我有用

【讨论】:

  • 在使用最低 API 级别 11 时这可能吗?我注意到您需要至少 API 14 才能删除操作栏上的图标,DarkActionBar 主题也是如此。
  • 是的,您需要最低 API 级别 14 才能以这种方式使用它
  • 已更新。图片没有占据整个屏幕宽度。
  • 在您的 imageView 中添加 scaleType:fitXY 或使用图像作为您的根布局的背景。另外,请使用match_parent 而不是fill_parent,因为它已被弃用。从您的相对布局中删除orientation,为什么会有&lt;View /&gt;?
猜你喜欢
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
相关资源
最近更新 更多