加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用XML和java代码混合控制UI界面

发布时间:2020-12-16 02:16:22 所属栏目:百科 来源:网络整理
导读:在XML中放比较固定的组件,在java代码中放变化比较多的复杂的组件 LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orien

在XML中放比较固定的组件,在java代码中放变化比较多的复杂的组件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

</LinearLayout>



public class MainActivity extends Activity {
	private ImageView iView[] = new ImageView[2];
	private int ImageID[] = new int[] { R.drawable.img01,R.drawable.img02 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
		LayoutParams params = new LayoutParams(253,148);
		for (int i = 0; i < ImageID.length; i++) {
			iView[i] = new ImageView(this);
			iView[i].setImageResource(ImageID[i]);
			iView[i].setPadding(5,5,5);
			iView[i].setLayoutParams(params);
			layout.addView(iView[i]);
		}

	}

	@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;
	}

}

注意

1:java.lang.OutOfMemoryError错误,加载4张图片竟然还会说内存不足。。。

2 : setContentView(R.layout.activity_main);在findViewByID后面出现会导致得到的View是null

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读