欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2020-11-18 00:56:30|栏目: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>

效果图

上一篇:Android 异步获取网络图片并处理导致内存溢出问题解决方法

栏    目:Android代码

下一篇:Android BaseAdapter应用实例

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

本文地址:http://www.codeinn.net/misctech/23842.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有