我在AlertDialog中将以下布局作为自定义视图 .
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:padding="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:columnCount="3">
<TextView
android:id="@+id/code1"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:gravity="center"
android:text="\u2022"
android:textSize="@dimen/match_code_digit_size"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/code2"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="\u2022"
android:textSize="@dimen/match_code_digit_size"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/code3"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:gravity="center"
android:text="\u2022"
android:textSize="@dimen/match_code_digit_size"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k1"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="1"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k2"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="2"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k3"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="3"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k4"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="4"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k5"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="5"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k6"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="6"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k7"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="7"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k8"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="8"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k9"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:text="9"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/k0"
android:layout_width="@dimen/match_code_button_size"
android:layout_height="@dimen/match_code_button_size"
android:layout_column="1"
android:text="0"
android:textSize="@dimen/match_code_button_text"
tools:ignore="HardcodedText" />
</GridLayout>
<TextView
android:id="@+id/error"
style="@style/ErrorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/match_error"
android:visibility="invisible" />
</LinearLayout>
<FrameLayout
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80ffffff"
android:visibility="visible">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>
对不起大量的布局,把它放在'原样' .
注意ID为 progress 的底部FrameLayout . 尽管它有 android:layout_height="match_parent" ,在设备上它看起来像"wrap_content" - 高度只匹配内部ProgressBar . 虽然在Android Studio设计器中显示得很完美,但却占据了整个视图 .
怎么了?
以下是AS设计师的布局
并在设备上(尝试了模拟器和真实设备,效果相同)
4 回答
尝试将根FrameLayout更改为RelativeLayout
你有两个FrameLayouts . @id进展1和
LinearLayout
一个 .首先快速浏览developer.android.com
那么对你的父母来说
FrameLayout
你有几个孩子?(2)你们俩的孩子都有引力吗?不,不是@id的进步
所以首先你添加线性布局,然后你添加FrameLayout所以..孩子
FrameLayout
应该在顶部,它是 . 那是你的问题吧?即使你的布局很长也可以缩短到这一点
Output (我在您的子框架布局中添加了缺少 gravity 的线性布局图像):
我有同样的问题,并设法解决它 . 似乎没有人注意到,但问题的根源是
GridLayout
. 正如您在蓝图中看到的那样,FrameLayout
没有问题,因为它确实与父级匹配 .GridLayout
的项目具有固定大小,这是错误的 . 您需要为GridLayout
中的每个项目设置权重,以便项目可以拉伸以适应任何屏幕分辨率 . 但是GridLayout
中的权重需要API 21.所以我建议你使用嵌套的LinearLayout's
,将item的权重设置为1 .我在我的项目中遇到的问题是,我有两个具有相同Id的Frame布局 . 解决方案是为帧布局分配唯一ID .
希望这有助于未来的读者 .