Using the XML and Java code blending control UI Interface

Source: Internet
Author: User

Main.xml ....... ........

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "@drawable/background"
Android:id= "@+id/layout"
>
</LinearLayout>


Activi ......... ..............

Package com.example.ui;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.ViewGroup.LayoutParams;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;

public class Mainactivity extends Activity {

Private ImageView [] img=new imageview[4]; Declares an array that holds a ImageView component
Private int[] Imagepath=new int[]{//Declare and initialize an array that holds access to the picture
R.drawable.j1,r.drawable.j2,r.drawable.j3,r.drawable.w1
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

LinearLayout layout= (linearlayout) Findviewbyid (r.id.layout);//Get a linear layout manager defined in an XML file
for (int i=0;i<imagepath.length;i++) {//
Img[i]=new ImageView (this);//Create a ImageView component
Img[i].setimageresource (Imagepath[i]);//Specify the picture to display for the ImageView component
Img[i].setpadding (5, 5, 5, 5);//Set the inner margin of the ImageView component
Layoutparams params=new Layoutparams (253,148);//Set the width and height of the picture
Img[i].setlayoutparams (params);//Set layout parameters for ImageView components
Layout.addview (Img[i]);//Add the ImageView component to the layout manager
}
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

}

Effects such as:

Using the XML and Java code blending control UI Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.