Android Studio开发基础之使用XML和Java代码混合控制UI界面


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:id="@+id/layout">
</LinearLayout>

package com.example.lhb.contentprovider1;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout LL=(LinearLayout)findViewById(R.id.layout);

        ImageView []Img=new ImageView[4];
        int []ImgSource=new int[]{
             R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4
        };

        for(int i=0;i<ImgSource.length;i++){
            Img[i]=new ImageView(this);
            Img[i].setImageResource(ImgSource[i]);
            Img[i].setPadding(4,4,4,4);
            ViewGroup.LayoutParams LP=new ViewGroup.LayoutParams(200,150);
            Img[i].setLayoutParams(LP);
            LL.addView(Img[i]);
        }
    }
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。