首页 > 代码库 > Android Dialog无法填满屏幕宽度问题解决

Android Dialog无法填满屏幕宽度问题解决

技术分享

就是将上面的自定义布局放到一个Dialog里面,布局xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="155dp"
    android:background="@color/transparent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingBottom="10dp"
    android:paddingTop="0dp">
    <LinearLayout android:id="@+id/sharelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/share_dialog_bg"
        android:orientation="vertical"
        android:padding="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
        <TextView android:id="@+id/wxtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="微信好友"
            android:textSize="16sp"
            android:textColor="@color/head_text"
            android:gravity="center"
            android:paddingTop="12dp"
            android:paddingBottom="12dp"/>
        <View
            android:layout_width="fill_parent"
            android:layout_height="1px"
            android:layout_alignParentBottom="true"
            android:background="#d1d1d1" />
        <TextView android:id="@+id/wxfriend"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="微信朋友圈"
            android:textSize="16sp"
            android:textColor="@color/head_text"
            android:gravity="center"
            android:paddingTop="12dp"
            android:paddingBottom="12dp"/>
    </LinearLayout>
    <TextView android:id="@+id/sharecancle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消"
        android:textSize="16sp"
        android:textColor="#FF4E17"
        android:background="@drawable/share_dialog_bg"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:layout_below="@id/sharelayout"/>
</RelativeLayout>

然后
View dialogview = LayoutInflater.from(activity).inflate(R.layout.customsharelayout, null);
shareDialog = new Dialog(activity, R.style.dialog_untran);
		shareDialog.setContentView(dialogview);
		Window window = shareDialog.getWindow();
		WindowManager.LayoutParams windowparams = window.getAttributes();
		window.setGravity(Gravity.BOTTOM);
		  Rect rect = new Rect();
		  View view1 = window.getDecorView();
		  view1.getWindowVisibleDisplayFrame(rect);
		  windowparams.height = DensityUtil.dip2px(activity, 155);
		  windowparams.width = SettingUtil.getDisplaywidthPixels();
		  window.setWindowAnimations(R.style.AnimationDialog);
		  window.setBackgroundDrawableResource(android.R.color.transparent);
		  window.setAttributes(
					(android.view.WindowManager.LayoutParams) windowparams);
		  shareDialog.show();

Animation设置

<style name="AnimationDialog">
        <item name="android:windowEnterAnimation">@anim/custom_dialog_enter</item>
        <item name="android:windowExitAnimation">@anim/custom_dialog_exit</item>
    </style>

custom_dialog_enter.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromYDelta="100%p"
        android:toYDelta="0" />
    <alpha
        android:duration="@android:integer/config_shortAnimTime"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>


主意是使用dialog设置在底部,并且宽度和padding值根据layout的布局来定。






Android Dialog无法填满屏幕宽度问题解决