Java 如何修复对话框的暗显和alpha效果
我希望我的android对话框看起来像: 但使用此xml:Java 如何修复对话框的暗显和alpha效果,java,android,android-layout,layout,Java,Android,Android Layout,Layout,我希望我的android对话框看起来像: 但使用此xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:alpha="0.20"
android:backgroundDimAmount="0.0">
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.1" />
<Button
android:id="@+id/socialBtn1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:scaleType="fitXY"
android:src="@android:color/tab_indicator_text"
android:text="@string/fb_share" />
导致此对话框:
问题:
(1) 按钮的alpha值应为1,但alpha值应为0.2
(2) 对话框bg的alpha值应为0.2,但似乎为alpha 1
(3) bg周围应为dim=0(1)按钮应为alpha=1,但似乎为alpha 0.2
这是默认的按钮背景。您必须使用按钮的android:background
属性提供特定背景。我认为android:src=“@android:color/tab\u indicator\u text”
不适用于按钮。该属性用于ImageView
(2)对话框背景的alpha应为0.2,但似乎为alpha 1
是否确定对话框背景的alpha值为0.2?通过查看设计,我猜alpha值应该是0.7-0.8
,结果是部分透明。而且您只设置了alpha。您还需要指定背景颜色。我建议使用dialog.getWindow().setBackgroundDrawable
方法动态地给出alpha值
例如:
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(178); //alpha 0.7 (0-250 range)
dialog.getWindow().setBackgroundDrawable(d);
dialog.show();
对话框布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="500dp"
android:backgroundDimAmount="0.0"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/socialBtn1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:scaleType="fitXY"
android:background="#0066FF"
android:layout_marginTop="10dp"
android:text=" Facebook " />
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.1" />
</LinearLayout>
结果显示以下对话框
你应该试试
Dialog mdialog =new Dialog(this);
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(55);
mdialog.getWindow().setBackgroundDrawable(d);
您必须设置背景图像或颜色以获得所需的效果 例如,如果使用颜色,则可以通过在Linearlayout属性中添加下线来实现对话框的半透明背景
android:background="#B2000000"
前两位数字决定透明度效果。
格式为#AARRGGBB,其中AA为alpha通道,RR为红色通道,GG为绿色通道,BB为蓝色通道。要计算AA,请将不透明度百分比乘以255。例如,对于30%的透明度,即70%的不透明度:255*0.7=178
,十六进制B2。因此,对于黑色半透明背景,颜色为#B2000000
与使用纯色按钮类似,可以设置颜色或自定义背景图像
android:background="@drawable/ic_button_bg"
我无法理解:如何使对话框背景部分透明?如今,唯一的按钮变成了部分透明,而这些是我唯一想保持完全不透明的按钮。我说过,使用
getWindow()动态地给出alpha。setBackgroundDrawable
方法,使用android:background
属性为按钮提供特定的颜色/可绘制。非半透明背景如何?我想要周围的环境clear@EladBenda:我假设您所说的“包围”仍然是指对话框内的区域,即按钮周围。如果是,则要移除上述示例中的半透明,请从#AARRGGBB中移除AA,因此对于黑色不透明背景,它将变成android:background=“#000000”。