练习代码,请无视命名规则等细节, ̄□ ̄||
使用alertDialog自定义view代码:
View view = View.inflate(getApplicationContext(),R.layout.dialog_biaozhi,null);
TextView tvShuoMing = view.findViewById(R.id.tv_shuoming);
ImageView imageView = view.findViewById(R.id.img_cuowu);
tvShuoMing.setText(datas.get(pos).getDescription());
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setView(view)
.create();
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
R.layout.dialog_biaozhi的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorWhite"
android:orientation="vertical">
<RelativeLayout
android:background="@color/colorMain"
android:layout_width="match_parent"
android:layout_height="35dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="说明"
android:textColor="@color/colorWhite"
android:textSize="16sp" />
<ImageView
android:id="@+id/img_cuowu"
android:src="@mipmap/img_cuowu"
android:layout_marginRight="20px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="20dp"
android:layout_height="20dp" />
</RelativeLayout>
<TextView
android:id="@+id/tv_shuoming"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:lineSpacingMultiplier="1.4"
android:text="表示禁止装载高度超过标志所示数值的车辆通行。表示禁止装载高度超过标志所示数值的车辆通行。表示禁止装载高度超过标志所示数值的车辆通行。" />
</RelativeLayout>
</RelativeLayout>
布局效果右侧:
顶部是蓝色的标题栏,但是实际上显示的时候,蓝色的标题栏两侧留有空白,但是我的代码是设置为了match_parent的,尝试过设置背景色为透明等方法无果,还是下图这样的情况:
两侧留有空白,百思不得其解后,终于找到了解决方法:
在alerDialog.show()方法后加上这句:
完美解决: