自定义视图导致RelativeLayout的奇怪行为

时间:2013-12-22 13:45:15

标签: android android-layout android-actionbar

我正在尝试使用Romain Guy的SpotlightView来突出我的Action Bar的某个部分,但我遇到了一些问题。首先,我创建了一个自定义操作栏,通过此布局将文本居中,这在真空中工作。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rel_test"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center" >

    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/app_name"
        android:textSize="24sp"
        android:textColor="@android:color/white"
        android:contentDescription="@string/app_name"
        android:shadowColor="@android:color/black"
        android:shadowDx="0"
        android:shadowDy="-1"
        android:shadowRadius="1" />
</RelativeLayout>

现在,这一切都很好,而且标题是中心的,你可以在这里看到。

enter image description here

然而,当我做以下事情时,一切都搞砸了。

public boolean onPrepareOptionsMenu(Menu menu) {
    SpotlightView spotlightView = new SpotlightView(this, null);

    // some slight changes I made to allow me to set information dynamically
    spotlightView.setTargetId(mActionbarTitle.getId());
    spotlightView.setMaskId(R.drawable.spot_mask);
    spotlightView.setAlpha(0.33f);
    RelativeLayout layout = (RelativeLayout) getSupportActionBar().getCustomView().findViewById(R.id.rel_test);
    layout.add(spotlightView);
}

发生以下情况时。请注意,没有显示任何内容,因为我已经将SpotlightView基本上剥离到构造函数中对super的调用,以及对super.onDraw()的调用。我还在屏幕截图中显示了这里显示的布局边界。

Incorrect ActionBar

正如您所看到的,看起来发生的事情是包裹RelativeLayout仅跨越后退按钮和操作按钮之间的空间,导致TextView仅在该空间中居中。但是,为什么会这样呢?它有什么办法吗?我是否需要覆盖onMeasure或其中一个View回调?

另外,有趣的是,当我向TextView添加另一个rel_test时,标题仍然正确居中。

编辑:好的,所以我把它缩小到调用setMaskId引起的问题,而Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8); Canvas c = new Canvas(a); c.drawBitmap(b, 0.0f, 0.0f, null); 又调用了这段代码:

c.drawBitmap(b, 0.0f, 0.0f, null);

当我注释掉{{1}}这一行时,一切顺利。但是,鉴于代码依赖于该画布,我不太清楚如何删除它。

我对Android上的图片并不熟悉,因此任何有关如何调整原始代码以创建相同聚光灯效果而不删除中心操作栏的建议将非常受欢迎。

0 个答案:

没有答案
相关问题