android 代码实现布局t字型布局怎么实现

下次自动登录
现在的位置:
& 综合 & 正文
android学习——GridView实现主界面布局
GridView(网格视图),用于在界面上按行、列分布的方式显示多个组件。GridView和ListView有相同的父类,因此他们具有相似的特性。他们的主要区别在于:ListView是在一个方向上分布;而GridView 是在两个方向上分布。
GridView提供的常用XML属性如下:
android:columnWidth
设置列的宽度
android:gravity
设置对其方式
android:horizontalSpacing
设置各元素键的水平间距
android:numColumns
android:stretchMode
设置拉伸模式
android:verticalSpacing
设置各元素之间的垂直间距
实现GridView布局主要分三个步骤:
1、在mian_interface.xml使用GridView布局。
&?xml version="1.0" encoding= "utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation="vertical" &
android:id= "@+id/gridview"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:columnWidth= "90dp"
android:gravity= "center"
android:horizontalSpacing= "6dp"
android:numColumns= "2"
android:stretchMode= "columnWidth"
android:verticalSpacing= "30dp" &
&/GridView &
&/LinearLayout&
2、在grid_items.xml中实现主界面的UI
&?xml version="1.0" encoding= "utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation="vertical" &
&!-- 显示的图片 --&
&ImageView
android:layout_height= "wrap_content"
android:id= "@+id/itemImage"
android:layout_width= "wrap_content"
android:src= "@drawable/gerenxinxiguanli"
android:layout_gravity= "center"
android:contentDescription= "@string/gerenxinxiguanli"&
&/ImageView &
&!-- 显示的文字 --&
android:text= "@string/gerenxinxiguanli"
android:id= "@+id/itemText"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"&
&/TextView &
&/LinearLayout&
3、接下来就是实现java代码了。
public class MainInterfaceActivity extends Activity
//将图片放入数组中
int[] mainInterfaceImageIDs=
R.drawable. gerenxinxiguanli,
R.drawable. xuankeguize,
R.drawable. yixuankecheng,
R.drawable. chengjichaxun,
R.drawable. kechengchaxun,
R.drawable. xuanke
//将图片对应的文字内容存入一个数组
String[] mainInterfaceStr =
"个人信息管理" ,
"选课规则",
"已选课程",
"成绩查询",
"课程查询",
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. mian_interface);
GridView gridView = (GridView) findViewById(R.id.gridview);
//生成动态数组,传入数据
ArrayList&HashMap&String, Object&& lstItems = new ArrayList&HashMap&String,Object&&();
for( int i
= 0;i & mainInterfaceImageIDs. length;i++){
HashMap&String, Object& map = new HashMap&String,
Object&();
map.put( "itemsImage", mainInterfaceImageIDs[i]);
map.put( "itemsText", mainInterfaceStr[i]);
lstItems.add(map);
SimpleAdapter imageItems = new SimpleAdapter( this,
lstItems, R.layout.grid_items ,
new String[]{ "itemsImage", "itemsText"}, new int[]{R.id. itemImage,R.id. itemText});
gridView.setAdapter(imageItems);
//当点击图片时进入相应的操作,暂时只实现了其中的两个其他的都相似。
gridView.setOnItemClickListener( new OnItemClickListener()
public void onItemClick(AdapterView&?&
arg, View v, int position, long id){
Intent intent = new Intent();
switch(position){
intent.setClassName( "com.example.mobileelectivesystem",
"com.example.mobileelectivesystem.GeRenXinXiGuanLiActivity" );
startActivity(intent);
intent.setClassName( "com.example.mobileelectivesystem",
"com.example.mobileelectivesystem.XuanKeGuiZeActivity" );
startActivity(intent);
Toast. makeText(getBaseContext(), "pic" +
(position + 1) + " selected" ,
Toast. LENGTH_SHORT).show();
&&&&推荐文章:
【上篇】【下篇】10777人阅读
android(18)
android 的Java代码中的布局相关方法LayoutParams
& & Android的界面布局可以用两种方法,一种是在xml中布局,一种是和JAVA中Swing一样在JAVA代码中实现Ui界面的布局,用xml的布局管理器布局是很方便的,但是在一些代码中需要动态的显示界面,这个时候xml就缺少了一种灵活性,使用XML和JAVA代码中布局可以解决这样的问题。在此只对JAVA代码中的LoyoutParams方法进行理解及使用。
& &&LoyoutParams类中主要保存的是布局参数,元素或组件可以通过这个类实现对布局参数的设置。在xml中它对应的是元素的”空间位置“、“大小”、“子元素在体内的空间位置”、“在父元素的填充方式”等属性。在xml队形的属性有(“&android:layout_width=&wrap_content&
& & & & android:layout_height=&wrap_content&
&&&&android:gravity=&center&
通过引用的代码说明LayoutParams在布局中的作用:
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
//调用addView()方法增加一个TextView到线性布局中
mLayout.addView(textView, p);
FrameLayout framelayout = new FrameLayout(this);
text=new TextView(this);//
Layoutparames params = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.WRAP_CONTENT //创建保存布局参数的对象
params.gravity = Gravity.CENTER_HORIZONTAN|Gravity.CENTER_VERTICAL;//设置居中显示
text.setLayoutParams(params);//设置布局参数
framelayout.addView(text);//将元素添加到布局管理器中
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:257214次
积分:1515
积分:1515
排名:千里之外
原创:18篇
评论:20条
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 t字型页面布局网页 的文章

 

随机推荐