当前位置:  编程技术>移动开发
本页文章导读:
    ▪TextView 兑现原理 实践        TextView 实现原理 实践 自定义View   [功能] 1. 自定义View 实现 TextView 的功能 2. 典型的 TextView 如下: <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:.........
    ▪ 将assets or raw folder资料复制到sd卡        将assets or raw folder文件复制到sd卡 有时候我们需要把程序的raw文件放在sd卡中,其实有时候这样做可以释放资源,有时候可能是使坏呼呼 void copyAssets() { String[] files; try { files.........
    ▪ 修订:更改listView中item不同状态上的颜色       修订:更改listView中item不同状态下的颜色 当更改每个item点击后出现不同的颜色的方法; <style name="Widget.AbsListView"> <item name="android:listSelector">@drawable/my_selector</item> </style>   .........

[1]TextView 兑现原理 实践
    来源: 互联网  发布时间: 2014-02-18
TextView 实现原理 实践

自定义View

 

[功能]

1. 自定义View 实现 TextView 的功能

2. 典型的 TextView 如下:

<TextView
			android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="HelloTextView" />

 

写道
android:text="HelloTextView"

 

就会显示指定的String

 

 

 

[代码]

1.  定义Text2View 所用关键字"text" 用于指定显示用的string

* 在 res/value 目录下创建 attrs.xml 文件 如下定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="Text2View">
        <attr name="text" format="string" />
    </declare-styleable>
</resources>

 

 

 

2. 定义 public class Text2View extends View

public class Text2View extends View {
    Paint text2Paint;
    String text2Text;
    int ascent;
    


    //Constructor - for java
    public Text2View(Context context) {
        super(context);
        initialize();
    }

    //Constructor - for xml
    public Text2View(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        initialize();

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.Text2View);
        
        int n = ta.getIndexCount();
        
        for(int i =0;i < n;i++){
        	int attr = ta.getIndex(i); 
        	
        	switch(attr){
        	case R.styleable.Text2View_text:
        		updateText(ta.getString(R.styleable.Text2View_text));
        		break;
        		
        	//TO ADD CUSTOM ATTRIBUTE
        		
        	default:
        		break;
        	}
        	
        }
        
        ta.recycle();
    }
    
    private final void updateText(String s){
    	text2Text = s;
    	
    	requestLayout();
        invalidate();
    }
    
    //load default setting on Text2View
    private final void initialize() {
        text2Paint = new Paint();
        text2Paint.setAntiAlias(true);
        text2Paint.setTextSize(16);
        text2Paint.setColor(0xFF000000);
        //setPadding(3, 3, 3, 3);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = (int) text2Paint.measureText(text2Text) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        ascent = (int) text2Paint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = (int) (-ascent + text2Paint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(text2Text, getPaddingLeft(), getPaddingTop() - ascent, text2Paint);
        
    }
}

 

 

3. Text2View 构造函数需要解释一下:

//Constructor - for java
    public Text2View(Context context) {
        super(context);
        initialize();
    }

    //Constructor - for xml
    public Text2View(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        initialize();

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.Text2View);
        
        int n = ta.getIndexCount();
        
        for(int i =0;i < n;i++){
        	int attr = ta.getIndex(i); 
        	
        	switch(attr){
        	case R.styleable.Text2View_text:
        		updateText(ta.getString(R.styleable.Text2View_text));
        		break;
        		
        	//TO ADD CUSTOM ATTRIBUTE
        		
        	default:
        		break;
        	}
        	
        }
        
        ta.recycle();
    }

 

 

4. 在 xml 文件中如何使用Text2View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.android.View"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
<TextView
			android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="HelloTextView" />
<com.android.View.Text2View
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            app:text="HelloText2View" />
</LinearLayout>

 

 

 

5. 效果图 和默认的TextView 比较一下

1 楼 yzhong_sa 2010-01-20  
这个  意义何在?
2 楼 gryphone 2010-01-20  
yzhong_sa 写道
这个  意义何在?


定制化View啊 再作为模板
3 楼 ansili 2010-01-21  
  很好,很强大。
最近急需这类知识。
4 楼 melode11 2010-01-22  
Lz实现个textView写那么长篇...不就是onMeasure和onDraw俩方法.
5 楼 melode11 2010-01-22  
另外真想订制textView的外观,可以继承textView.重写onDraw
6 楼 gryphone 2010-01-23  
melode11 写道
Lz实现个textView写那么长篇...不就是onMeasure和onDraw俩方法.

那你知道系统的TextView 实现的原理么? 这就是我写这个的本意!
7 楼 wjb_forward 2010-03-12  
写得很好,但是,能不能在关键的地方加点注释啊,看不大懂
8 楼 yzhong_sa 2010-03-28  
对于做自己的ui确实有一些提示
9 楼 yyw8316zpp 2010-03-29  
这方面的前辈们,俺是接触开发的新手,刚进公司,求这方面的发展,谁能给点资料,越基础越好。谢谢。
10 楼 wjb_forward 2010-04-01  
哎呀,居然没看太懂,麻烦把源码发出来看看吧,最好把注释写详细点,谢谢
11 楼 javetu_7 2010-07-28  
TextView上怎么显示光标?
12 楼 kldwq2002 2011-01-10  
为什么不懂还装蛋的回复这么多呢?
13 楼 luckwangjing 2011-01-10  
建议LZ下载android源代码!里面有详细的实现代码。
14 楼 zhs2472 2011-01-11  
写得不错,能明白一些原理,期待楼主多写点这类的,加油

    
[2] 将assets or raw folder资料复制到sd卡
    来源: 互联网  发布时间: 2014-02-18
将assets or raw folder文件复制到sd卡

有时候我们需要把程序的raw文件放在sd卡中,其实有时候这样做可以释放资源,有时候可能是使坏呼呼

void copyAssets() 
{ 
    String[] files; 
    try 
    { 
        files = this.getResources().getAssets().list(""); 
    } 
    catch (IOException e1) 
    { 
        return; 
    } 
 
    if(!mWorkingPath.exists()) 
    { 
        if(!mWorkingPath.mkdirs()) 
        { 
            new AlertDialog.Builder(this) 
                .setTitle(R.string.ERROR) 
                .setMessage(R.string.FAILED_DIR_CREATE) 
                .setPositiveButton(android.R.string.ok, new OnClickListener(){ 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) 
                    { 
                        dialog.dismiss(); 
                    } 
                }) 
                .create() 
                .show(); 
        } 
    } 
 
    for(int i = 0; i < files.length; i++) 
    { 
        try 
        { 
            String fileName = files[i]; 
 
            if(fileName.compareTo("images") == 0 || 
               fileName.compareTo("sounds") == 0 || 
               fileName.compareTo("webkit") == 0) 
            { 
                continue; 
            } 
 
            File outFile = new File(mWorkingPath, fileName); 
            if(outFile.exists()) continue; 
 
            InputStream in = getAssets().open(fileName); 
            OutputStream out = new FileOutputStream(outFile); 
 
            // Transfer bytes from in to out 
            byte[] buf = new byte[1024]; 
            int len; 
            while ((len = in.read(buf)) > 0) 
            { 
                out.write(buf, 0, len); 
            } 
 
            in.close(); 
            out.close(); 
        } 
        catch (FileNotFoundException e) 
        { 
            e.printStackTrace(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
    } 

 


    
[3] 修订:更改listView中item不同状态上的颜色
    来源: 互联网  发布时间: 2014-02-18
修订:更改listView中item不同状态下的颜色

当更改每个item点击后出现不同的颜色的方法;

<style name="Widget.AbsListView"> <item name="android:listSelector">@drawable/my_selector</item> </style>

 


 my_selector在drawable

<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@color/transparent" /> <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> <item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" /> </selector>

 


 我想在代码中实现

public View getView(final int position, View convertView, ViewGroup parent) { int colorPos = position % colors.length; ... convertView.setBackgroundColor(colors[colorPos]); return convertView; }

 


 

可是没有成功 是不是没有刷新呢 画面呢

 

根据上面都没有成功,我看了好多有的成功有的没有成功不知道怎么弄的 就连第一种也没成功

color.xml

<resources> 
    <drawable name="list_normal">#96FFFFFF</drawable> 
    <drawable name="list_active">#66000000</drawable> 
    <drawable name="list_pressed">#CA000000</drawable> 
</resources> 

 

listview_background.xml
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_enabled="false" android:state_focused="true" 
        android:drawable="@drawable/list_normal" /> 
  <item android:state_pressed="true" 
        android:drawable="@drawable/list_pressed" /> 
  <item android:state_focused="true" 
        android:drawable="@drawable/list_active" /> 
</selector> 

还有的呢使用

然后

 

On ListView, set android:listSelector="@android:color/transparent".

************************************************************************

本来这个问题就留在这里的,不过那天一个网友一直向实现而不能实现,我就和他一起讨论很久试了很多方法,最后发现无论怎么样都是系统的颜色,后来我发现我们用的是listAvtivity,根本没有使用xml中的listActivty,所以我就把那些样式用在了主题当中结果可行 如下面的附件。

如果想按照以前上面的那些方法,那必须不能用listActivity本身的activity,或者直接用activity从里面找到lisView

总之你想用上面的方法 就是***上面的方法,我想你必须用你自己的listVIew而不是系统的。这只是我的猜想没有去实现嘿嘿。

 

最后又通过修改getView 证明也是可一的。

 

public class StyledListItems extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        setListAdapter(new StyledListItemAdapter(this));
    }
    
    
    private class StyledListItemAdapter extends BaseAdapter {
        public StyledListItemAdapter(Context context) {
            mContext = context;
        }

        public int getCount() {
            return mTitles.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                sv = new StyledItemView(mContext, mTitles[position]);
            } else {
                sv = (StyledItemView) convertView;
            }

            sv.setContent(mTitles[position]);
            return sv;
        }

        private Context mContext;
        
        private String[] mTitles = 
        {
                "lorem dipsum",   
                "lorem dipsum",
                "lorem dipsum",       
                "lorem dipsum",
                "lorem dipsum",
                "lorem dipsum",  
                "lorem dipsum",
                "lorem dipsum"
        };
        
    }
    
    private class StyledItemView extends LinearLayout {
        private LayoutInflater mInflater;

		public StyledItemView(Context context, String title) {
            super(context);
            this.setOrientation(VERTICAL);
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout layout = (LinearLayout)mInflater.inflate(R.layout.list_item, null);
            mTitle = (TextView)layout.findViewById(R.id.txt_item);
            addView(layout);
        }

        public void setContent(String title) {
            mTitle.setText(title);
        }

        private TextView mTitle;
    }
    
    StyledItemView sv;
    
}

 

list——tem

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/div_list_item"
	android:background="@drawable/list_item_style"
	android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
	
	<TextView 
		android:id="@+id/txt_item"
		android:layout_width="fill_parent"
		android:textSize="14sp"
    	android:layout_height="20dip" />
       
</LinearLayout>

 android:background="@drawable/list_item_style"
就是要变化的背景

 

The items in ListView is TextView, as below:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/phone_numbers_row_text_tv"
xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_centerHorizontal="true"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/list_view_selector"
   android:gravity="center"
   />

The "list_view_selector" is as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item
               android:state_selected="true"
               android:drawable="@drawable/number_select_bg"/>
       <item
               android:state_pressed="true"
               android:drawable="@drawable/number_select_bg"/>
       <item
               android:state_focused="true"
               android:drawable="@drawable/number_select_bg"/>
       <item
               android:drawable="@drawable/number_bg"/>
</selector>


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3