发现AlertDialog内部自定义View宽高有问题时,我的布局layout如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/left"
android:layout_weight="1"
android:layout_gravity="center"/>
<com.example.mytstapplication.MyView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/right"
android:layout_weight="1"
android:layout_gravity="center"/>
</LinearLayout>
此时,布局显示如下,嘻嘻,这完全没毛病,这就是我想要的布局,于是我运行程序
得到了如下布局,不嘻嘻
造成这个问题的原因是,AlertDialog的内部根布局宽高默认是wrap_content
也就是说我的l布局layout其实是这样的
修改了以后布局果然和我手机上显示的一样
然后把高度不对的那个组件高度设置为wrap_content就可以了
android:layout_height=“wrap_content”