安卓自定义----带Edit的TextView标签组件

   组件效果图如下, 组件包含两种显示方式, 第一种是TextView和EditText横排显示, 第二种是TextView和EditText竖排显示 :

  主activety_main.xml内容, 组件包含两种显示方式, 分别是horizontal 和 vertical, 所以还需要两个次要的布局文件:

运行下面代码

复制代码
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".main">

    <com.demo0.nono.labeledit.LabelEditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        labelText="名字1"
        labelFontSize="30"
        labelPosition="left"
    />

    <com.demo0.nono.labeledit.LabelEditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        labelText="名字2"
        labelFontSize="50"
        labelPosition="top"
        />
</LinearLayout>
复制代码

  组件包含两种显示方式, 分别是horizontal 和 vertical, 所以还需要两个次要的布局文件 horizontal.xml

运行下面代码

复制代码
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
复制代码

  vertical.xml

运行下面代码

复制代码
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
复制代码

  

  自定义的组件类代码:

运行下面代码

复制代码
package com.demo0.nono.labeledit;

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

/**
 * Created by nono on 2018/4/7.
 */
public class LabelEditText extends LinearLayout{
    private TextView textview;
    private String labelText;
    private String labelPosition;
    private int labelFontSize;

    public LabelEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        int resId = attrs.getAttributeResourceValue(null, "labelPosition", 0);
        if(resId != 0) {
            //如果能够获取到资源ID, 那就通过资源ID获取资源中配置的字符串
            labelPosition = getResources().getString(resId);
        }else{
            labelPosition = attrs.getAttributeValue(null, "labelPosition");
        }
        Log.d("lablePositionValue", labelPosition);
        resId = attrs.getAttributeResourceValue(null, "labelText", 0);
        if( resId != 0 ) {
            labelText = getResources().getString(resId);
        }else{
            labelText = attrs.getAttributeValue(null, "labelText");
        }

        resId = attrs.getAttributeResourceValue(null, "labelFontSize", 0);
        if( resId != 0 ) {
            labelFontSize = getResources().getInteger(resId);
        }else{
            labelFontSize = attrs.getAttributeIntValue(null, "labelFontSize", 14);
        }
        LayoutInflater li;
        li = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        if("left".equals(labelPosition)) {
            li.inflate(R.layout.horizontal, this);
        }else{
            li.inflate(R.layout.vertical, this);
        }
        textview = (TextView)findViewById(R.id.textView);
        textview.setText( labelText );
        textview.setTextSize((float)labelFontSize);
    }
} 
复制代码

  EOF

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/8733329.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(1079)  评论(0编辑  收藏  举报
历史上的今天:
2015-04-07 属性类型:数据类型,访问器类型的坑

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示