AlertDialog とカスタム コンテンツ/ビューで問題に直面しています。簡単に言うと、ソフトキーボードが開いているときに AlertDialog のサイズは変更されません。次のスクリーンショットは、私の問題と達成したいことを最もよく示しています。
現在の動作 (左) と必要な動作 (右)
SOで同様の問題を抱えている他のスレッドがいくつかあることを私は知っています。残念ながら、提供されたソリューションはどれもうまくいきませんでした。私のサンプルコードに従ってください:
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
</ScrollView>
Java - CustomAlertDialog.class
public class CustomAlertDialog extends AlertDialog{
private Context context;
public CustomAlertDialog(Context context) {
super(context);
this.context = context;
}
public void buildDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_content, null);
builder = new AlertDialog.Builder(context);
builder.setTitle("EditText");
builder.setView(layout);
builder.setPositiveButton("Ok", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
dismiss();
}
});
builder.create();
}
public void showDialog(){
builder.show();
}
}
上記のクラス/関数は、私の Main.java クラスでボタンを押すと呼び出されます
btnAlertDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CustomAlertDialog dialog = new CustomAlertDialog(Main.this);
dialog.buildDialog();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
dialog.showDialog();
}
});
私は次のことを試しました:
このように LinearLayout にスクロールバーを追加する
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
結果:何も変わらない
ダイアログのフラグの設定:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
結果:何も変わらない
カスタム スタイルを AlertDialog に適用する:
<style name="AlertDialog" parent="@android:style/Theme.Holo.Light">
<item name="android:windowFullscreen">false</item>
</style>
ジャワ
builder = new AlertDialog.Builder(context, R.style.AlertDialog);
これはうまくいきました。残念ながら、次のスクリーンショットで確認できるいくつかの問題が発生しました
この問題に何日も苦労してきたので、助けていただければ幸いです。ありがとう!
解決
現在、次のメソッドを使用して Dialog を作成していCustomAlertDialog.class
ます。以前に機能しなかった理由の詳細については、 nandeeshの回答の下にある私のコメントを参照してください。
public void startDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_content, null);
builder = new AlertDialog.Builder(context);
builder.setTitle("EditText");
builder.setView(layout);
builder.setPositiveButton("Ok", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
dismiss();
}
});
AlertDialog aDialog = builder.create();
aDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
aDialog.show();
}