I know that the activity always has an xml layout file, but without this xml, I simply implement it directly in the code using Java. That is to say, I am not writing this xml for layout. Instead, we can directly generate a java class. Some friends may ask, is this necessary? Writing a java class by yourself is not the same function as xml layout. But in fact, in many cases, our interface is not completely unchanged. If a view changes a lot, the original xml becomes a weakness. And once we want to encapsulate a written activity into a JAR package, the redundant xml may become cumbersome. To solve this problem, I did a test myself. I feel that the effect can solve this problem easily. Therefore, we can use it as a reference.
In the override onCreate method in an activity, we must specify the layout file in setContentView (R. layout. XXXX); now we discard the specified xml.
Java code:
Java code
Import android. app. Activity;
Import android. content. Context;
Import android. content. res. Resources;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. drawable. BitmapDrawable;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TextView;
Public class IndexActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
NewView view = newView. buildView (IndexActivity. this );
// The original place where the xml needs to be specified is replaced by the object for creating the corresponding Layout
SetContentView (view );
Resources res = getResources ();
// Specify the loaded image
Bitmap bmp = BitmapFactory. decodeResource (res, R. drawable. back_1 );
BitmapDrawable bmp 1 = new BitmapDrawable (bmp );
View. imageView. setImageDrawable (BMP 1 );
}
// The LinearLayout layout is tested here. Other layout s have not been tried yet, but the basic principle is similar.
Static class newView extends LinearLayout {
Private ImageView imageView;
Public newView (Context c ){
Super (c );
/*
* Only ImageView is created here, for example, TextView EditText Button.
* And so on. All created here is added to addView (). The layout rules created here are the same as those created in addView ().
* The rules for xml layout are the same.
*/
ImageView = new ImageView (c );
/*
* Multiple parameters such as size, height, and color can be set. These methods are easy to use in Apis provided by android.
* Find and list several common
* TextView mTitle = new TextView (c );
* // Add text
* MTitle. setText (title );
* // Center
* MTitle. setGravity (Gravity. CENTER );
* // Font color
* MTitle. setTextColor (Color. WHITE );
*
* AddView actually has multiple constructor methods. You need to add the following frequently used methods to address location change requirements:
* AddView (mTitle, new LinearLayout. LayoutParams (
* LayoutParams. WRAP_CONTENT,
* LayoutParams. WRAP_CONTENT, Gravity. CENTER_HORIZONTAL ));
*/
AddView (imageView );
/*
* For the attribute settings of LinearLayout, we can directly PASS Parameters Using the method provided after inheritance.
* For example: setBackgroundResource (imgId); setPadding (0, 8, 0, 8 );
*/
}
Public static newView buildView (Context c ){
Return new newView (c );
}
}
}
Author "2528"