今天写了一个关于label的显示窗口,代码量也挺简单的,大家一看就明白。不管在哪里我们做软件的都需要用到label

先让我们看一下图先。

 

android—label窗口——基础编_休闲

 

  1. package com.smart.widget; 
  2.  
  3. import android.content.Context; 
  4. import android.util.AttributeSet; 
  5. import android.view.LayoutInflater; 
  6. import android.widget.LinearLayout; 
  7. import android.widget.TextView; 
  8.  
  9. import com.smart.acitivy.R; 
  10.  
  11. public class LabelEdit extends LinearLayout{ 
  12.  
  13.     private TextView textView; 
  14.     private String labelText; 
  15.     private int labelFontSize; 
  16.     private String labelPosition; 
  17.  
  18.     public LabelEdit(Context context, AttributeSet attrs) 
  19.     { 
  20.         super(context, attrs); 
  21.         //  读取labelText属性的资源ID 
  22.         int resourceId = attrs.getAttributeResourceValue(null"labelText"0); 
  23.         //  未获得资源ID,继续读取属性值 
  24.         if (resourceId == 0
  25.             labelText = attrs.getAttributeValue(null"labelText"); 
  26.         //  从资源文件中获得labelText属性的值 
  27.         else 
  28.             labelText = getResources().getString(resourceId); 
  29.         //  如果按两种方式都未获得labelTex属性的值,表示未设置该属性,抛出异常 
  30.         if (labelText == null
  31.         { 
  32.             throw new RuntimeException("必须设置labelText属性."); 
  33.         } 
  34.         //  获得labelFontSize属性的资源ID 
  35.         resourceId = attrs.getAttributeResourceValue(null"labelFontSize"0); 
  36.         //  继续读取labelFontSize属性的值,如果未设置该属性,将属性值设为14 
  37.         if (resourceId == 0
  38.             labelFontSize = attrs.getAttributeIntValue(null"labelFontSize"
  39.                     14); 
  40.         //  从资源文件中获得labelFontSize属性的值 
  41.         else 
  42.             labelFontSize = getResources().getInteger(resourceId); 
  43.         //  获得labelPosition属性的资源ID 
  44.         resourceId = attrs.getAttributeResourceValue(null"labelPosition"0); 
  45.         //  继续读取labelPosition属性的值 
  46.         if (resourceId == 0
  47.             labelPosition = attrs.getAttributeValue(null"labelPosition"); 
  48.         //  从资源文件中获得labelPosition属性的值 
  49.         else 
  50.             labelPosition = getResources().getString(resourceId); 
  51.         //  如果未设置labelPosition属性值,将该属性值设为left 
  52.         if (labelPosition == null
  53.             labelPosition = "left"
  54.          
  55.         String infService = Context.LAYOUT_INFLATER_SERVICE; 
  56.         LayoutInflater li; 
  57.         //  获得LAYOUT_INFLATER_SERVICE服务 
  58.         li = (LayoutInflater) context.getSystemService(infService); 
  59.         LinearLayout linearLayout = null
  60.         //  根据labelPosition属性的值装载不同的布局文件 
  61.         if("left".equals(labelPosition)) 
  62.             linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_horizontal, this); 
  63.         else if("top".equals(labelPosition)) 
  64.             linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_vertical, this); 
  65.         else 
  66.             throw new RuntimeException("labelPosition属性的值只能是left或top."); 
  67.          
  68.         //  下面的代码从相应的布局文件中获得了TextView对象,并根据LabelTextView的属性值设置TextView的属性 
  69.         textView = (TextView) findViewById(R.id.textview); 
  70.         textView.setTextSize((float)labelFontSize);          
  71.         textView.setTextSize(labelFontSize); 
  72.         textView.setText(labelText); 
  73.  
  74.     } 
  75.  

我看一下Label类
package com.smart.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smart.acitivy.R;

public class LabelEdit extends LinearLayout{

    private TextView textView;
    private String labelText;
    private int labelFontSize;
    private String labelPosition;

    public LabelEdit(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        //  读取labelText属性的资源ID
        int resourceId = attrs.getAttributeResourceValue(null, "labelText", 0);
        //  未获得资源ID,继续读取属性值
        if (resourceId == 0)
            labelText = attrs.getAttributeValue(null, "labelText");
        //  从资源文件中获得labelText属性的值
        else
            labelText = getResources().getString(resourceId);
        //  如果按两种方式都未获得labelTex属性的值,表示未设置该属性,抛出异常
        if (labelText == null)
        {
            throw new RuntimeException("必须设置labelText属性.");
        }
        //  获得labelFontSize属性的资源ID
        resourceId = attrs.getAttributeResourceValue(null, "labelFontSize", 0);
        //  继续读取labelFontSize属性的值,如果未设置该属性,将属性值设为14
        if (resourceId == 0)
            labelFontSize = attrs.getAttributeIntValue(null, "labelFontSize",
                    14);
        //  从资源文件中获得labelFontSize属性的值
        else
            labelFontSize = getResources().getInteger(resourceId);
        //  获得labelPosition属性的资源ID
        resourceId = attrs.getAttributeResourceValue(null, "labelPosition", 0);
        //  继续读取labelPosition属性的值
        if (resourceId == 0)
            labelPosition = attrs.getAttributeValue(null, "labelPosition");
        //  从资源文件中获得labelPosition属性的值
        else
            labelPosition = getResources().getString(resourceId);
        //  如果未设置labelPosition属性值,将该属性值设为left
        if (labelPosition == null)
            labelPosition = "left";
       
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;
        //  获得LAYOUT_INFLATER_SERVICE服务
        li = (LayoutInflater) context.getSystemService(infService);
        LinearLayout linearLayout = null;
        //  根据labelPosition属性的值装载不同的布局文件
        if("left".equals(labelPosition))
            linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_horizontal, this);
        else if("top".equals(labelPosition))
            linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_vertical, this);
        else
           
            throw new RuntimeException("labelPosition属性的值只能是left或top.");
       
        //  下面的代码从相应的布局文件中获得了TextView对象,并根据LabelTextView的属性值设置TextView的属性
       
        textView = (TextView) findViewById(R.id.textview);
        textView.setTextSize((float)labelFontSize);           
        textView.setTextSize(labelFontSize);
        textView.setText(labelText);

    }

}