[英]Creating Android Transparent Layout in Custom Alert Dialog
I have written a Custom Dialog, which works perfectly fine. 我写了一个自定义对话框,它工作得很好。
public class GalleryAlertDialog extends Dialog {}
In a class I call the class with the 在课堂上,我用课堂打电话给班级
dialog.show();
I want to achieve this look: 我希望实现这个目标:
I need the button over the dialog,like this. 我需要对话框上的按钮,就像这样。
However, this is what I get: 但是,这就是我得到的:
Here's the layout xml's part where I declare the layout and the button inside it: 这是布局xml的部分,我在其中声明布局和其中的按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_buttonContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dip"
android:gravity="right"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_button_popupclose"
android:contentDescription="TODO"
android:tag="btn_close" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_pagerContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_buttonContainer"
android:layout_marginTop="80dip"
android:background="@color/white" />
<ToggleButton
android:id="@+id/tbFavorite"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_gravity="bottom|right"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dip"
android:background="@drawable/selector_check"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ff212121"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/llContactFullName"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@color/white"
android:gravity="left|center_vertical"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="@color/seperator" />
<TextView
android:id="@+id/txtContactFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:maxLines="1"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="20sp" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:maxLines="1"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<TextView
android:id="@+id/txtCompany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:ellipsize="end"
android:maxLines="1"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<TextView
android:id="@+id/txtPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:maxLines="1"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
..... .....
I tried giving the ll_buttonContainer a transparent background programatically too. 我试着以编程方式给ll_buttonContainer一个透明的背景。
llButtonContainer.setBackgroundColor(mContext.getResources().getColor(
R.color.transparent_tr));
llButtonContainer.setBackgroundColor(Color.argb(0, 0, 0, 0));
I even tried 我甚至试过了
android:background="null"
Nothing works. 什么都行不通。 The layout is still visible, it gets any color (red, yellow, white etc.) other than transparent.
布局仍然可见,它获得除透明之外的任何颜色(红色,黄色,白色等)。 Is this how it works, will I not be able to give it transparent background due to how Dialog works?
它是如何工作的,由于Dialog如何工作,我是否无法给它透明的背景?
Thanks for any help. 谢谢你的帮助。
Ok I found the answer myself. 好的,我自己找到了答案。
I found this line on StackOverFlow, tried it and it worked. 我在StackOverFlow上找到了这一行,尝试了它并且它有效。 Writing this line,
写这条线,
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
just before 就在此之前
alertDialog.show();
Hope this helps some one and you won't suffer as much as I do. 希望这有助于一个人,你不会像我一样受苦。 It's pretty simple, but very annoying if you miss it.
这很简单,但是如果你错过它会很烦人。
Use this Style into yout res/values/style 将此样式用于res / values / style
making WindowTranparent 制作WindowTranparent
<style name="Theme.Transparent" parent="android:Theme">#03afee
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Apply this to your Dialog... 将此应用于您的对话...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#00000000"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<ImageView
android:id="@+id/ad_view_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:src="@android:drawable/ic_delete" />
</RelativeLayout>
</RelativeLayout>
in dialog
final Dialog dialog = new Dialog(context);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.ad_view);
dialog.setCancelable(false);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
ImageView closeBtn = ((ImageView) dialog.findViewById(R.id.closeButton));
ImageView addView=((ImageView) dialog.findViewById(R.id.ad_view_image));
addView.setImageBitmap(loadedImage);
closeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
addView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(marketlink.startsWith("http")){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketlink));
mContext.startActivity(browserIntent);
dialog.dismiss();
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.