【问题标题】:android header opacityandroid标题不透明度
【发布时间】:2012-11-15 07:48:06
【问题描述】:

我正在尝试构建类似的带有透明标题的列表视图,例如Attaching a fixed, transparent, header to a ListView? 但是我怎样才能改变标题不透明度? 我尝试在颜色代码中使用 alpha,例如:“#00bebebe”,但没有奏效。

我的标题背景“title_bar_background”

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Top color -->
<item android:bottom="20dip">
    <shape android:shape="rectangle">
        <solid android:color="#bebebe"
             /> 
    </shape>
</item>

<!-- Bottom color -->
<item android:top="20dip">
    <shape android:shape="rectangle">
        <solid android:color="#696969" /> 
    </shape>
</item>
</layer-list>

我的标题布局“custom_title”

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
.... 
android:background="@drawable/title_bar_background"
android:id="@+id/customLayout"
>

还有我的 listView,其中我包含 custom_title

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
..... >


<include  
     layout="@layout/custom_title"
     android:layout_height="40dip"
     android:layout_width="fill_parent"/>

这项改变不透明度的工作

View backgroundImg = findViewById(R.id.mycustomLayout);
    Drawable background = backgroundImg.getBackground();
    background.setAlpha(40);

但只有当我不将该布局包含在其他布局中时它才有效。但是,当我在另一个布局中包含该布局时,我怎么能设法设置不透明度? (就像我上面的 xml 布局)

【问题讨论】:

    标签: android header opacity


    【解决方案1】:

    通过 id 查找布局并通过设置 Alpha 更改不透明度。

    【讨论】:

      【解决方案2】:

      只需通过它的 id 找到您的标题布局,然后这样做:

      LinearLayout headerLayout = (LinearLayout)findViewById(R.id.layoutHeader); 
      AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
      alpha.setDuration(0); // Make animation instant
      alpha.setFillAfter(true); // Tell it to persist after the animation ends
      // And then on your layout
      headerLayout.startAnimation(alpha);
      

      还有View.setAlpha();但从 API 级别 11 开始。

      编辑:

      我们认为您的布局在另一个布局中:

      <RelativeLayout android:id="@+id/parentLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      <ImageView ...
      bla bla bla />
      
      <LinearLayout android:id="@+id/transparentLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" >
      ... your views
      </LinearLayout>
      ...some other views
      
      </RelativeLayout>
      

      那么你就这样做了:

      LinearLayout mytransparentLayout = (LinearLayout) findViewById(R.id.transparentLayout);
      mytransparentLayout.setAlpha(0.5f);
      

      【讨论】:

      • 我发现 setAlpha 可以很好地改变不透明度。但问题是,如果我在其他布局中包含该布局(NullPointerExc),我不能这样做。知道如何解决这个问题吗?
      • 所有你需要的是为你的布局指定一个 id,即使它在其他布局中,然后通过 id 找到它,并应用 AlphaAnimation 或 setAlpha() 方法。查看我的编辑。
      猜你喜欢
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2013-01-19
      相关资源
      最近更新 更多