本篇记载自定义 AlertDialog 以及遇到的问题

自定义 AlertDialog

先上效果图
Android自定义AlertDialog及设置宽高无效问题

创立布局

首要咱们要创立想要的 dialog 布局文件

<?xml version="1.0" encodingAPP="utf-8"?>
<LinearLahttp 404yout xmlns:aappointmentndroid="http://schemhttp协议as.android.com/apk/res/android"
android:layout_widandroidstudio装置教程th="match_parent"
android:layout_height=APP"200dp"
android:backgroappleund="@drawandroid手机able/style_linearlayout_round_bg"
android:orientatioandroid体系n="vertical">
<Lineandroid是什么手机牌子arLayout
android:layout_width="match_parent"
android:layouhttp 302t_height="0dp"
android:layout_weapproachight="1">
<TextView
android:layout_width="100dp"
android:layout_height="match_applicationparent"
android:text="@string/penaltyType"
android:gravity="center"
android:textColor="@color/colorAappointmentccent" />
&ltapple;Spinner
android:id="@+id/sp_penalty"
anapp装置下载droid:layout_width="matcandroid的drawable类h_parent"
android:android是什么手机牌子layout_heighandroid体系t="wrap_content"
android:entries="http://192.168.1.1登录@array/penaltyArray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layouthttpwatch_height="0dp"
andappstoreroid:layout_weight="1">
<TextView
ahttp署理ndroid:layandroid/yunosout_widtandroid什么意思h="100dpappear"
androiappstored:layout_height="matcandroid体系h_parent"
android:text="@string/penaltyContent"
android:gravity="center"
android:textColor="@color/colorAccent" />
<EditText
android:android下载装置id="@+id/et_penaltyConteandroid的drawable类nt"
android:android什么意思layout_wappleiappreciatedth="matcandroid体系h_pareAndroidnt"
android:layout_height="wrap_content"
android:backghttp://192.168.1.1登录round="@nullappstore"
android:inputTAndroidypandroid体系e="text" />
</LinearLayout>
<TextViehttps和http的区别w
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/gray" />
<Linearandroid是什么手机牌子Layout
android:layout_width="approachmatch_appearparent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:visibility="invisible" />
<appointmentButton
android:id="@+id/btn_save"
android:layout_width="wrhttpclientap_content"
androhttp://192.168.1.1登录id:layout_height="32dp"
androHTTPid:background="@drawable/style_button_round_bg"
android:textColor="@color/white"
android:text="@string/save" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:visibility="invisible" />
<Button
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:background="@drawandroid什么意思able/style_button_round_bg"
android:textandroid下载装置Color="@color/white"
android:text="@string/close" />
<TextView
androiandroid平板电脑价格d:layout_wandroid体系idth="0dp"
android:layout_height="mhttpwatchatch_parent"
android:layout_weight="0.6"
android:visibility="invandroid手机isible" /&android平板电脑价格gt;
</LinearLayout>
</LinearLayout>

咱们要把 AlertDialog 做成这个姿势,然后创立好自定义布局之后,就要在 Activity/Fragmandroid下载ent 中进行操作了。

增加布局android平板电脑价格

在 Activity 中把appreciate创立好的布局增加到咱们的 AlertDialog 中,就能够了。

        View customPenaltyView = LayoutInflater.from(mContext).approveinflate(R.layout.widget_custom_select_penalty, null, false);
.android手机.....
// 设置自定义布局
dialoapplicationg.setView(customPenaltyView);
// 也能够经过这种办法设置布局
Objects.requireNonNull(dialog.getWindow()).setContentView(customPenaltyView);

http 500儿之所以会有两种办法,是因为网上有些自定义 Dialog 会在运用榜首种办法设置布局的时分,把布局填充到了 Dialog 的 Tiappeartle 和 Button 之间的区域,这种状况下,运用第二中办法能够解决。(Ps:还没有遇到过),一般状况下,都是运用榜首种办法。

增加监听

布局设置Android好之后,就对布局上的控件做一些监听处理基本上就能够了。

        final AlertDialog dialoapp装置下载gandroidstudio装置教程 = new AlertDialog.Builder(mContext).create();
finaapplel Spinnapp装置下载er phttp协议enaltyType = customPenaltappearanceyView.findViewByIdhttp://192.168.1.1登录(R.id.sp_penalty);
fhttp 500inal EditTandroid什么意思ext penaltyContent = customPenaltyappearanceView.findViewById(R.httpclientid.et_penaltyContent);
final String [] penaltyTypeArray = getResources().getStringArray(R.array.penaltyArray);
final String[] penaltyTypeString = new String[1];
dialog.setView(customPeappearancenaltyView);
Button save  = customPenaltyVandroid的drawable类iew.findViewById(R.id.btn_save);
Button close  = customPenaltyVappearanceiew.android体系findViewappstoreById(R.id.btn_closandroid什么意思e);
phttp署理enaltyType.setOnItemSelectedLishttps和http的区别tener(new Adaandroid是什么手机牌子pterView.OnItemSelectedListener() {
@Override
public void onItemSelHTTPected(AdapterView<?> parent, View view, int positihttp协议on, long id) {
penaltyHTTPTypeStrapproveing[0] = penaltyTypeArray[position];
}
@Override
public void onNothinandroid下载装置gSelected(AdapterView<?>http署理 parent) {
}
});
save.setOnClickListener(new Vieappearancew.OnClickListehttp署理ner() {
@Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(mContext, penaltyTypeString[0] + penaltyContent.geapprovetText().appointmenttoString(), Toast.LENGTH_SHORT).show();
}
});
close.setOnClickListenehttp://192.168.1.1登录r(new Viewapp装置下载.OnClickListener() {
@Override
public vohttpwatchid onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Objects.requireNonNull(dialog.getWindow()).http://192.168.1.1登录setBackgroundDrawableResourappointmentce(R.color.transparent);

总结

有了上面的三步,基本上就能够把 AlertDIalog 做成想要的姿HTTP态。

也能够简练一点,这样做。

		// 自定义的布局
View addPlayerView = LayoutInflater.from(mContext).inflate(R.layout.widget_custom_alertdialogandroid下载_addplayer, nuandroid什么意思ll, false);
// 把布局增加到dialog
final AlertDialog dialog = new AlertDialog.Builder(mContext, R.style.AlertDiaappearancelog_AppCompat_Transparent).setView(addPlayerView).create(http 404);
// 监听
final EditText addPlayerappointmentName = addappointmentPlayerView.findViewByIdandroid下载装置(R.id.http://192.168.1.1登录et_addPlayerName);
TextView addPlayerData = addPlayerView.findViewById(R.id.tv_addPlayerButton);
addPlayerData.setOnClickLisandroid手机tener(new View.OnClickListener()app装置下载 {
@Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(TruthOrDareActivity.this, "增加玩家" + addPlayerName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
dialog.showandroid手机();

终究便是遇到的问题

  1. 布局填充区域不对

approve增加布HTTP局中,把布局文件填充到 Dialog 的一部分区域中,可http署理以经过 Dialog 的 getWindow 的 shttpclientetContentView 办法来设置,能够看上面的第二步,增加布局。

  1. 设置宽高失效

在布局文件中设置宽高失效的问题,依然能够在 Window 中设置相关特点来完成。咱们能够获取屏幕高宽,然后设置想要的方位。

  1. 布局文件要用相对布局

布局文件http://192.168.1.1登录运用 RelativeLayout,假设运用线性布局 LinearLayout 会导APP致自定义 xml 的android下载装置巨细(宽高)无法调度

  1. RelativeLayout 设置高度无效的问题

在运用相对布局设置高度时,高度无效的问题,其实并不是 layout_height 无效,而是它根据子控件里,最大的控件高度来设置的。
意味着:在最外层的 RelativeLayout 中设置高度为 match_parent 或许详细的高appear度,假设子控件设置了高度是 match_parent,且子控件的高度小于最外层的最大高度时,就会呈现失效的状况。其实,并没有失效,而是他根据子控件http 302中的最大高度来设置的。就相当approach于 wrap_parent。
参看:
自定义 Dialog
自定义 Dialog 宽高无效