欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android界面布局之实现文本块布局效果示例

程序员文章站 2022-06-04 12:18:30
复制代码 代码如下:package cn.aibow.android.layoutdemo1; import android.os.bundle;import andro...

复制代码 代码如下:

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>

效果图

android界面布局之实现文本块布局效果示例