Recent study android, here there is a small problem, open the application on the flash back what is the reason?
package com.example.linearlayouttest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
public class LinearLayoutTest extends Activity {
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_linear_layout_test);
LinearLayout root = (LinearLayout) findViewById (R.id.root);
Button bn1 = (Button) findViewById (R.id.bn1);
bn1.setText (R.string.bn1);
root.addView (bn1);
Button bn2 = (Button) findViewById (R.id.bn2);
bn2.setText (R.string.bn2);
root.addView (bn2);
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.linear_layout_test, menu);.
return true;
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
android: gravity = "bottom | center_horizontal"
android: id = "@ + id / root"
>
android: id = "@ + id/bn1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string/bn1"
/>
android: id = "@ + id/bn2"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string/bn2"
/>
android: id = "@ + id/bn3"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string/bn3"
/>
android: id = "@ + id/bn4"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string/bn4"
/>
Reply:
The error message posted about ah
Reply:
LinearLayout root = (LinearLayout) findViewById (R.id.root);
root.addView (bn1);
root.addView (bn2);
More than three commented
Reply:
You can use the software to repair it
Reply:
Oh ah, because if directly in java in a direct new components only need addView eh?
Reply:
2nd Floor positive solution, addView belong to dynamic loading, and
setContentView (R.layout.activity_linear_layout_test); has a static loaded.
That is, the control has a static load, and there is no need static loading.
Reply:
Reply:
Layout can not be achieved dynamically modify
root.addView (bn1);
Reply:
Can be dynamically loaded, but you have to re-set a btn the id, otherwise it will error
No comments:
Post a Comment