Android Studio開發基礎之使用XML和Java程式碼混合控制UI介面
阿新 • • 發佈:2019-01-04
<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]); } } }