2018年1月21日日曜日

android inflate先のレイアウトのないinflate

android inflate先レイアウトのないinflate

2018 Jan. 21.


サンプル1

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.my_view, null);
setContentView(myView);


サンプル2

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View titleView = inflater.inflate(R.layout.title_of_alert, null);
                builder.setCustomTitle(titleView);
                View descriptionView = inflater.inflate(R.layout.view_of_alert, null);
                builder.setView(descriptionView) ;
                builder.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Log.v("ok","Hi");
                        }
                    });
                AlertDialog dialog = builder.create();
                dialog.show();

            }
        });
    }
}

0 件のコメント:

コメントを投稿