Android:제목 없이 대화 상자를 만드는 방법은 무엇입니까?
안드로이드에서 사용자 지정 대화 상자를 생성하려고 합니다.다음과 같은 대화상자를 만듭니다.
dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);
대화 상자의 제목을 제외하고는 모두 정상적으로 작동합니다.대화 상자의 제목을 설정하지 않더라도 대화 상자 팝업에는 대화 상자 위치에 빈 공간이 있습니다.
대화 상자의 이 부분을 숨길 수 있는 방법이 있습니까?
AlertDialog로 시도해봤는데 레이아웃이 제대로 설정되지 않은 것 같습니다.
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);
dialog = builder.create();
((TextView) dialog.findViewById(R.id.nr)).setText(number);
이 코드를 사용하면 마지막 줄에 null 포인터 예외가 표시됩니다.대화 상자가 null이 아니므로 검색하려는 TextView가 없습니다.
Dialog Constructor(대화 상자 생성기)를 사용하는 부분에 대해 주석을 달면 대화 상자 레이아웃 위의 제목을 제외하고 모든 것이 정상적으로 작동합니다.
FEATURE_NO_TITLE은 다음과 같이 처음부터 대화 상자를 만들 때 작동합니다.
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
그러나 AlertDialog를 만들 때(또는 Builder를 사용할 때)는 이미 제목을 비활성화하고 내부적으로 사용자 지정 제목을 사용하기 때문에 작동하지 않습니다.
SDK 소스를 살펴봤는데, 해결이 안 될 것 같습니다.따라서 상단 간격을 제거하는 유일한 방법은 Dialog 클래스를 직접 사용하여 IMO에서 사용자 지정 대화 상자를 만드는 것입니다.
또한 styles.xml과 같은 스타일로 이 작업을 수행할 수 있습니다.
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
그리고 나서:
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
다음을 사용하여 대화 상자의 제목을 숨길 수 있습니다.
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
이 답변의 이전 버전으로, 너무 복잡합니다.
다음을 사용해야 합니다.AlertDialog
Android Developer's 사이트에 사용자 지정 대화 상자에 대한 좋은 설명이 있습니다.
매우 간략하게 요약하자면, 공식 웹사이트에서 복사한 아래와 같은 코드로 이 작업을 수행합니다.사용자 지정 레이아웃 파일을 가져와서 파일을 부풀리고 기본 텍스트와 아이콘을 제공한 다음 파일을 만듭니다.그면보거예요줄여러▁it거예요▁you로 보여줍니다.alertDialog.show()
.
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
논평에 대한 답변:
가 있 것. 는 TextView 가nr
있는 에 부리고있 에보음있기는풀이 .View view = inflater....
만약 그렇다면, 당신은 단지 한 부분만 변경하면 됩니다: 대신.dialog.findView...
, 해내다, 해보다view.findView...
builder.create는 dialog.show는 builder.show입니다.
코드에 이 행을 추가합니다.
requestWindowFeature(Window.FEATURE_NO_TITLE);
또는 XML에서 테마 사용
android:theme="@android:style/Theme.NoTitleBar"
XML은 코드 버전과 마찬가지로 더 나은 구현이 될 것입니다. 제목 표시줄이 생성된 다음 제거되므로 리소스가 낭비됩니다.
좋아요, 시도는 잘했지만 작동하지 않습니다.알겠습니다: 안드로이드.view.WindowManager$BadTokenException:창을 추가할 수 없습니다. 대화 상자를 표시하려면 응용 프로그램에 대한 토큰 null이 아닙니다.
경고 대화 상자 유형을 시스템 대화 상자(예: TYPE_SYSTEM_OVERLAY)로 변경하고 문제가 해결되는지 확인합니다.
다음과 같이 사용:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
대화 상자 창에서 제목 표시줄이 제거됩니다.
앞에 아래 setcontentview
:-
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
참고: 위의 코드는 순서와 라인이 같아야 합니다.requestWindowFeature
setContentView 행 앞에 있어야 합니다.
제목을 제거할 수 있습니다.
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
여기서 dialog는 내 대화 상자의 이름입니다.
에서 코에서다사경우할용을 requestWindowFeature(Window.FEATURE_NO_TITLE);
이 에반시전 앞에 .dialog.setContentView();
그렇지 않으면 응용 프로그램이 중단됩니다.
나는 이것을 하는 세 가지 방법을 찾았습니다 >
요청 창 기능 사용
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
스타일(style.xml) 사용
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
AndroidManifest.xml에서 XML 테마 사용
android:theme="@android:style/Theme.NoTitleBar"
에 Custom_Dialog.java를 합니다.requestWindowFeature(Window.FEATURE_NO_TITLE)
public class Custom_Dialog extends Dialog {
protected Custom_Dialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
requestWindowFeature(Window.FEATURE_NO_TITLE); //This line
}
}
올리비에그의 대답은 저에게 효과가 있었고 만약 사용자 정의 대화 상자 클래스를 만드는 것이 당신이 가고 싶은 경로라면 최고의 해결책입니다.하지만 AlertDialog 클래스를 사용할 수 없다는 것이 저를 괴롭혔습니다.기본 시스템 AlertDialog 스타일을 사용할 수 있기를 원했습니다.사용자 지정 대화 상자 클래스를 만드는 데 이 스타일이 없습니다.
그래서 커스텀 클래스를 만들지 않고도 작동할 수 있는 솔루션(해킹)을 찾았습니다. 기존의 빌더를 사용할 수 있습니다.
알림 대화상자는 제목에 대한 자리 표시자로 내용 보기 위에 보기를 배치합니다.뷰를 찾아서 높이를 0으로 설정하면 공간이 사라집니다.
지금까지 2.3과 3.0에서 테스트해 본 결과, 아직 모든 버전에서 작동하지 않을 수 있습니다.
다음은 두 가지 도우미 방법입니다.
/**
* Show a Dialog with the extra title/top padding collapsed.
*
* @param customView The custom view that you added to the dialog
* @param dialog The dialog to display without top spacing
* @param show Whether or not to call dialog.show() at the end.
*/
public static void showDialogWithNoTopSpace(final View customView, final Dialog dialog, boolean show) {
// Now we setup a listener to detect as soon as the dialog has shown.
customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Check if your view has been laid out yet
if (customView.getHeight() > 0) {
// If it has been, we will search the view hierarchy for the view that is responsible for the extra space.
LinearLayout dialogLayout = findDialogLinearLayout(customView);
if (dialogLayout == null) {
// Could find it. Unexpected.
} else {
// Found it, now remove the height of the title area
View child = dialogLayout.getChildAt(0);
if (child != customView) {
// remove height
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
lp.height = 0;
child.setLayoutParams(lp);
} else {
// Could find it. Unexpected.
}
}
// Done with the listener
customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
// Show the dialog
if (show)
dialog.show();
}
/**
* Searches parents for a LinearLayout
*
* @param view to search the search from
* @return the first parent view that is a LinearLayout or null if none was found
*/
public static LinearLayout findDialogLinearLayout(View view) {
ViewParent parent = (ViewParent) view.getParent();
if (parent != null) {
if (parent instanceof LinearLayout) {
// Found it
return (LinearLayout) parent;
} else if (parent instanceof View) {
// Keep looking
return findDialogLinearLayout((View) parent);
}
}
// Couldn't find it
return null;
}
다음은 사용 방법의 예입니다.
Dialog dialog = new AlertDialog.Builder(this)
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, true);
DialogFragment를 합니다.onCreateDialog
위의 상자를 합니다.그런 다음 위의 첫 번째 예와 같이 대화 상자를 만들고 반환합니다.유일한 변경 사항은 대화 상자에서 show()를 호출하지 않도록 세 번째 매개 변수(show)로 false를 전달해야 한다는 것입니다.DialogFragment는 다음과 같습니다.
예:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new AlertDialog.Builder(getContext())
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, false);
return dialog;
}
추가 테스트를 통해 필요한 추가 수정 사항을 업데이트할 것입니다.
이 질문이 아직도 실제인지는 모르겠지만, 제 경우, Dialog에서 DialogFragment로 전환했을 때,
requestWindowFeature(Window.FEATURE_NO_TITLE);
선택사항은 아니었지만, 저는 사용할 수 있었습니다.
setStyle(STYLE_NO_TITLE, 0);
대신에 같은 결과를 가지고 있습니다.
작성기를 사용하여 제목을 빈 문자열로 설정합니다.
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("");
...
builder.show();
전체 대화 상자의 "중력" 속성을 "중심"으로 설정합니다.그런 다음 중앙에 배치하지 않을 대화 상자의 모든 하위 구성 요소로 설정을 재정의해야 합니다.
dialog=new Dialog(YourActivity.this, 1); // to make dialog box full screen with out title.
dialog.setContentView(layoutReference);
dialog.setContentView(R.layout.layoutexample);
XML에서 테마 사용
android:theme="@android:style/Theme.NoTitleBar"
만약 우리가 단순히 대화상자를 사용하지 않는다면,setTitle()
,그럼 제목의 공간을 없애는 데 효과가 있을까요?
mUSSDDialog = new AlertDialog.Builder(context).setView(dialogView)
.setPositiveButton(R.string.send_button,DialogListener)
.setNegativeButton(R.string.cancel,DialogListener)
.setCancelable(false).create();
이제 이걸 사용할 수 있을 것 같습니다.
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.setTitle("")
.create()
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
제목 없는 대화 상자를 만듭니다.
public static AlertDialog showAlertDialogWithoutTitle(Context context,String msg)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setMessage(msg).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return alertDialogBuilder.create();
}
AlertDialog 사용 중, 사용 안 함setTitle()
제목을 사라지게 합니다.
여러 번의 해킹 끝에, 저는 이것을 작동시켰습니다.
Window window = dialog.getWindow();
View view = window.getDecorView();
final int topPanelId = getResources().getIdentifier( "topPanel", "id", "android" );
LinearLayout topPanel = (LinearLayout) view.findViewById(topPanelId);
topPanel.setVisibility(View.GONE);
사용하지 않고도 이 작업을 수행할 수 있습니다.AlertDialog
확장되는 새 클래스를 정의함으로써Dialog
다음과 같은 클래스:
public class myDialog extends Dialog {
public myDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
다음은 당신이 할 수 있는 것입니다.AlertBuilder
제목을 삭제하려면:
TextView title = new TextView(this);
title.setVisibility(View.GONE);
builder.setCustomTitle(title);
사용
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(true);
dialog.setContentView(R.layout.image_show_dialog_layout);
dialog_custom.requestWindowFeature(윈도우).FEATURE_NO_TITLE);
컷섬 대화 상자에서 제목을 제거합니다.
내용을 추가하기 전에 이 줄을 추가합니다.예를 들어
dialog_custom = Dialog(activity!!)
dialog_custom.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog_custom.setContentView(R.layout.select_vehicle_type)
dialog_custom.setCanceledOnTouchOutside(false)
dialog_custom.setCancelable(true)
Windows 기능(Windows)을 요청합니다.FEATURE_NO_TITLE);
하지만 저를 위해 일하지는 않습니다. 저와 같다면 이렇게 하십시오.
대화상자에 테마를 전달하면 제목 표시줄을 제거할 수 있습니다.
<style name="NoTitleDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
테마를 대화 상자로 전달합니다.
Dialog dialog = new Dialog(이것은 R.style)입니다.제목 대화상자 없음);
그것은 매우 간단합니다.
언급URL : https://stackoverflow.com/questions/2644134/android-how-to-create-a-dialog-without-a-title
'programing' 카테고리의 다른 글
오류와 함께 npm 설치 오류: ENOENT, chmod (0) | 2023.08.05 |
---|---|
하나의 블록에 여러 개의 시도 코드가 있음 (0) | 2023.08.05 |
sqlalchemy back_pumulates는 언제 사용해야 합니까? (0) | 2023.08.05 |
전체 폴더 및 내용을 삭제하는 방법은 무엇입니까? (0) | 2023.07.31 |
장고에서 양식에 대한 테스트를 어떻게 작성해야 합니까? (0) | 2023.07.31 |