I just create the Layou开发者_StackOverflow中文版t design using java code only not from the XML Layout Designs. Like the following
LinearLayout llay=new LinearLayout(this);
TextView tv1=new TextView(this);
tv1.setText("New Layout");
llay.addView(tv1);
How do use the LayoutInflater inflater for my LinearLayout ??? I am new to Android
The LayoutInflater
is used to get and split a layout defined via XML in your code. What I want to say is basically that you certainly don't need layout inflating if you define your layout via Java code like you've posted.
If you want to pass your LinearLayout
as your content view then set it in your onCreate()
method like this.
setContentView(llay);
Create a xml layout which you want to inflate as, a parent linearlayout and Textview tv1 and use this layout as inflater in MainActivity and use setContentView(main.xml) in onCreate method;
LinearLayout llay=new LinearLayout(this);
LinearLayout lytContainer = (LinearLayout) View.inflate(
this, R.layout.inflate_xml, null);
lytContainer.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
TextView tv1 = (TextView) lytContainer
.findViewById(R.id.tv1);
llay.addView(tv1);
精彩评论