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

ad1f045220b14b79a9cdec39474c40f9.jpgmain.xml....................

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/background"

android:id="@+id/layout"

>

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];   //声明保存一个ImageView组件的数组

private int[] imagePath=new int[]{                              //声明并初始化一个保存访问图片的数组

R.drawable.j1,R.drawable.j2,R.drawable.j3,R.drawable.w1

};

[email protected]

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

LinearLayout layout=(LinearLayout)findViewById(R.id.layout);//获取个xml文件中定义的线性布局管理器

for(int i=0;iimg[i]=new ImageView(this);//创建一个ImageView组件

img[i].setImageResource(imagePath[i]);//为ImageView组件指定要显示的图片

img[i].setPadding(5, 5,5, 5);//设置ImageView组件的内边距

LayoutParams params=new LayoutParams(253,148);//设置图片的宽度和高度

img[i].setLayoutParams(params);//为ImageView组件设置布局参数

layout.addView(img[i]);//将ImageView组件添加到布局管理器中

}

}

[email protected]

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;

}

}

效果如下图:

ad1f045220b14b79a9cdec39474c40f9.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值