android界面布局之实现文本块布局效果示例
package cn.aibow.android.layoutdemo1;
import android.os.bundle;
import android.app.activity;
import android.view.menu;
import android.view.motionevent;
import android.view.view;
import android.widget.textview;
import android.widget.toast;
public class mainactivity extends activity {
private int[] idarr = new int[]{r.id.tv1,r.id.tv2,r.id.tv3,r.id.tv4,r.id.tv5,r.id.tv6,r.id.tv7,r.id.tv8};
private int[] colorarr = new int[]{0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff};
private int[] bgarr = new int[]{0xffff6666,0xff1e67c0,0xffd47756,0xff5a626f,0xffee7434,0xff3eadeb,0xff0385fd,0xff00a179};
private string[] textarr = new string[]{"通讯电脑","生活厨卫","家用电器","日用百货","母婴玩具","户外体育","汽车配件","其它商品"};
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
for(int i=0;i<idarr.length;i++){
textview tv = (textview)findviewbyid(idarr[i]);
tv.settext(textarr[i]);
tv.setbackgroundcolor(bgarr[i]);
tv.settextcolor(colorarr[i]);
tv.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
textview t = (textview)v;
showmessage("您点击的是 : "+t.gettext().tostring());
}
});
}
}
private void showmessage(string msg){
toast.maketext(this, msg, toast.length_long).show();
}
@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;
}
}
布局xml
<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:orientation="vertical"
android:padding="6dp"
tools:context=".mainactivity" >
<linearlayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<textview
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
<textview
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<textview
android:id="@+id/tv3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
<textview
android:id="@+id/tv4"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<textview
android:id="@+id/tv5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
<textview
android:id="@+id/tv6"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<textview
android:id="@+id/tv7"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
<textview
android:id="@+id/tv8"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:text="文本"
android:gravity="center_vertical|center_horizontal"
android:textcolor="#ffffffff"
android:textsize="28sp"
android:background="#ff000000"/>
</linearlayout>
</linearlayout>
效果图