我曾经遇到过类似的问题,一个客户想要一个应用程序的演示,整个屏幕都要变得更白(他们称之为“透明”),除了通过一个覆盖在按钮上的对话框来解释按钮。
幸运的是,你的布局不像我处理的那个那么复杂 :)
现在,你可以用两种方法实现透明效果,一种是设置白色背景并调用所有视图的setAlpha()方法,另一种是创建半透明的白色覆盖层。
如果你选择使用覆盖层,你将需要找到一种方法通过覆盖层显示不透明的按钮。这可能会有点复杂。
如果你选择第一种选项,你只需要在不透明的视图上调用setAlpha(1)来显示它。
setAlpha()方法仅在api版本11以上可用,所以如果你的目标版本较早,你可能需要以稍微复杂的方式实现。
在Honeycomb之前的版本中设置视图透明度的示例:
为你的按钮创建布局(任何样式都可以,只要让它们相似,这样你就可以循环遍历它们):
<LinearLayout
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:tag="image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tile"/>
<TextView
android:tag="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:text="button1"/>
</LinearLayout>
在您的程序中,当您想要使按钮透明时:
LinearLayout l = (LinearLayout) findViewById(R.id.button1);
((ImageView)l.findViewWithTag("image")).setAlpha(0x7F);
((TextView)l.findViewWithTag("text")).setTextColor(0x7F000000);
当您决定如何创建透明效果时,您将不得不决定如何显示覆盖文本/气泡。您很可能希望将其放在整个布局的顶部的单独图层中,以确保它不受新视图的影响。
实现此目的的一种方法是将根布局元素更改为FrameLayout,然后在其中创建/显示。例如:
<FrameLayout background="#FFFF"> <!-- white background, just in case -->
<LinearLayout>
<!-- the rest of your layout -->
</LinearLayout>
<LinearLayout visibility="gone"> <!-- this will be your overlay view -->
<ImageView /> <!-- the arrow/ring -->
<TextView /> <!-- the description -->
</LinearLayout>
</FrameLayout>
当显示介绍时,您将隐藏的覆盖视图的位置设置为要解释的表项的位置,将文本更改为适当的字符串/资源并显示该视图。
当介绍结束时,您将所有按钮的alpha值重置,并将覆盖的可见性再次设置为gone。