Layout From Java Android Sdk
Solution 1:
LayoutSample.java
publicclassLayoutSamplerextendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(newSampleLayoutDemo(this));
}
}
SampleLayoutDemo
classSampleLayoutDemoextendsLinearLayout {
publicSampleLayoutDemo(Context context) {
super(context);
TextViewview=newTextView(context);
view.setText("Sample");
view.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addView(view);
setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
Create a class which extends Layout and call the layout in the activity setContentView() method.
Solution 2:
in onCreate you say:
myView = newMyView(this);
setContentView(myView);
myView.requestFocus();
If you want to draw your own graphics then in java file you write:
publicclassMyViewextendsView{
publicMyView(Context context){
super(context);
}
@OverridepublicvoidonDraw(Canvas canvas){
}
}
You can also set that MyView class extends LinearLayout etc and then use myView.addView() to create a nested layout.
Solution 3:
I understand your question as "I want to use a LinearLayout or other existing ViewGroup in my Activity and then add views to that without using XML files". Is this correct? If you want to do custom views ChristianB already stated the basics. Custom layouts are quite tricky and I recommend subclassing existing viewgroups.
So if my assumption about your question is correct, you create a view in your activity:
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayoutlayout=newLinearLayout (this);
TextViewhelloText=newTextView(this);
helloText.setText("Hello World");
layout.addView(helloText);
setContentView(layout);
}
You can exchange LinearLayout for any other ViewGroup, or skip the ViewGroup completely and add a single "fullscreen" view.
Post a Comment for "Layout From Java Android Sdk"