例如我今天想修改一个dialog中的控件距离顶部和右侧的距离:
那个控件的xml代码:

 <RelativeLayout
                android:id="@+id/dialog_content"
                android:layout_width="320dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right|top"
                android:layout_marginTop="78dp"
                android:layout_marginRight="25dp"
                android:background="@drawable/background_tips"
                android:orientation="vertical"
                android:padding="15dp"
                app:layoutWidth="@{fragment.width}">


                <TextView
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_gravity="center_horizontal"
                    android:background="@color/color_background_transparent"
                    android:text="@{fragment.content}"
                    android:textColor="@color/color_text_gray_33"
                    android:textSize="@dimen/text_size_22" />

 </RelativeLayout>

这个控件名称是dialog_content;
它的父亲控件是:dialog_layout:

<LinearLayout
            android:id="@+id/dialog_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/color_background_transparent"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/dialog_content"
                android:layout_width="320dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right|top"
                android:layout_marginTop="78dp"
                android:layout_marginRight="25dp"
                android:background="@drawable/background_tips"
                android:orientation="vertical"
                android:padding="15dp"
                app:layoutWidth="@{fragment.width}">


                <TextView
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_gravity="center_horizontal"
                    android:background="@color/color_background_transparent"
                    android:text="@{fragment.content}"
                    android:textColor="@color/color_text_gray_33"
                    android:textSize="@dimen/text_size_22" />

            </RelativeLayout>

</LinearLayout>

这个dialog用java代码创建:
在这里插入图片描述上图中的dynamicAlterLocation(top,end)就是用来动态改变控件在布局中位置的;
在这里插入图片描述
上图dialogContentView就是dialog_content在java中的引用名;
可以看出想改变一个控件在布局中的位置,就必须创建一个该控件的父控件的布局LayoutParams,
这个layoutParams也是该控件本身getLayoutParams()到的LayoutParams;
拿到这个正确的LayoutParams之后才能去调用layoutParams去修改位置。

接下来说下dialog的注意的点,调用dialog内部的控件时,必须先show出dialog才能调用,不然根本拿不到这个控件,会报空指针错误。说下原因:
dialog在new 的时候 并不会执行 oncreateView() 方法 因此里面的控件都未 初始化 而调用dialog.show()的时候才会执行oncreateView() 方法,就是这个原因,下次注意了,不要在这上面浪费时间。
我这次就是先调用的dynamicAlterLocation函数,后才show的dialog导致dialogContentView一直报空指针异常,后面看了日志才知道应该先show再来调用会使用到dialog内部控件的函数。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐