[Android] 自定义 Dialog 布局设置固定宽高无效

[Android] 自定义 Dialog 布局设置固定宽高无效,第1张

Dialog 的自定义布局的根布局的宽度是写固定的,显示的时候宽度和高度不是对应的固定值。

根布局外面又添加了一层 FrameLayout,设置其宽高均为 wrap_content 来包裹以前的布局。

这个时候猜测是否因为添加自定义视图的时候,布局参数被改写了,然后开始查看源码,最终发现确实是这样的。

在下面的源码分析中,最终发现也是用了 mWindow.setContentView(mAlertDialogLayout) 将 R.layout.alert_dialog.xml 的默认布局添加到 PhoneWindow, 和Activity一样的。

关键的地方看一下 setupCustomContent() 这个方法,在添加自定义视图的时候布局参数设置为 MATCH_PARENT 了,所以我们设置固定大小是没有作用的,要套一层父布局解决这个问题。

dev cardview设置固定行高需要在FrameLayout里面设置,因为是继承关系。

具体设置代码如下:

CardView.LayoutParams layoutParams = (CardView.LayoutParams)

catCard.getLayoutParams()

layoutParams.height = 10

这样就设置了固定高度为10个像素。

1 .FrameLayout简介

设计FrameLayout是为了显示单一项widget。通常,不建议使用FrameLayout显示多项内容;因为它们的布局很难调节。不用layout_gravity属性的话,多项内容会重叠;使用layout_gravity的话,能设置不同的位置。layout_gravity可以使用如下取值:

top :将对象放在其容器的顶部,不改变其大小.

bottom:将对象放在其容器的底部,不改变其大小.

left:将对象放在其容器的左侧,不改变其大小.

right:将对象放在其容器的右侧,不改变其大小.

center_vertical:将对象纵向居中,不改变其大小.

 垂直对齐方式:垂直方向上居中对齐。

fill_vertical:必要的时候增加对象的纵向大小,以完全充满其容器. 

垂直方向填充

center_horizontal:将对象横向居中,不改变其大小. 

水平对齐方式:水平方向上居中对齐

fill_horizontal:必要的时候增加对象的横向大小,以完全充满其容器. 

水平方向填充:center

将对象横纵居中,不改变其大小.

fill:必要的时候增加对象的横纵向大小,以完全充满其容器.

clip_vertical:附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.

垂直方向裁剪

clip_horizontal

附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.

水平方向裁剪

注意: 区分“android:gravity”和“android:layout_gravity”。

android:gravity   :是对控件本身来说的,是用来设置“控件自身的内容”应该显示在“控件自身体积”的什么位置,默认值是左侧。

android:layout_gravity :是相对于控件的父元素来说的,设置该控件在它的父元素的什么位置

2. FrameLayout示例

创建一个activity,包含2组FrameLayout:1组设置android:layout_gravity属性,另1组不设置android:layout_gravity属性。

layout文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <!-- 示例1 FrameLayout内容重叠 -->

    <TextView

        android:text="示例1, FrameLayout内容重叠"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <FrameLayout

        android:layout_width="300dp"

        android:layout_height="80dp" >

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="我是TextView,内容比较长"

            android:background="#ff0000"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="#ffff00"

            android:text="我是按钮"/>

    </FrameLayout>

    <!-- 示例2 FrameLayout使用layout_gravity属性 -->

    <TextView

        android:text="示例2, 设置layout_gravity"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <FrameLayout

        android:layout_width="300dp"

        android:layout_height="120dp" >

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="文本居左"

            android:background="#ff0000"

            android:gravity="center"

            android:layout_gravity="left"

            android:layout_margin="10dp"/>

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="文本居中"

            android:background="#ffff00"

            android:gravity="center"

            android:layout_gravity="center"/>

    </FrameLayout>

</LinearLayout>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11306171.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存