Android 一个可折叠的文本控件

2016-12-13  本文已影响552人  miaozbetter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="10dip"    
   android:orientation="horizontal">
  <LinearLayout    
    android:layout_width="0dp"    
    android:layout_height="wrap_content"    
    android:layout_weight="1"    
    android:layout_gravity="left"    
    android:gravity="left"    
    android:orientation="horizontal">    
    <TextView        
      android:id="@+id/desc_tv" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:gravity="center_vertical"        
      android:textColor="#4A4A4A" 
      android:lineSpacingExtra="3dp" 
      android:textSize="12sp"
      android:text="@string/def_content" />
  </LinearLayout>
  <LinearLayout    
    android:id="@+id/desc_op_ll" 
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:layout_gravity="right" 
    android:visibility="gone" 
    android:padding="10dp"> 
    <ImageView
      android:id="@+id/desc_op_iv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"        
      android:gravity="center"
      android:src="@mipmap/menu_pull_down1"/>
  </LinearLayout>   
</LinearLayout>       

2、布局创建完成,接下来开始编写自定义控件。首先在构造方法中获取需要的布局。

 public CollapsibleTextView(Context context, AttributeSet attrs) {
      super(context, attrs);    
      //获取布局   
      View view = inflate(context,R.layout.collapsible_textview_layout,this);    
      //文本内容
      tvDesc = (TextView)view.findViewById(R.id.desc_tv);   
      //上、下拉箭头布局      
      llDescOp = (LinearLayout) view.findViewById(R.id.desc_op_ll);
      //上、下拉箭头   
      ivDescOp = (ImageView) findViewById(R.id.desc_op_iv);
      llDescOp.setOnClickListener(this);
}

3、布局获取完成了,这时候需要将我们获取的布局添加到控件上。这时候我们需要重写方法onLayout。

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);    
    //文本的行数大于规定值,折叠并显示下拉箭头
    if(tvDesc.getLineCount() > TEXT_MAX_LINE){
        tvDesc.setMaxLines(TEXT_MAX_LINE);
        llDescOp.setVisibility(VISIBLE); 
        IS_COLLAPSIBLE = true;
        OLLAPSIBLE = true;
       }else{
            llDescOp.setVisibility(GONE);
            IS_COLLAPSIBLE = false;       
       }    
     }
}

4、接下来需要监听下拉箭头的点击事件了,通过判断文本框的状态做出相应的反应。

@Override
public void onClick(View view) {    
     post(new InnerRunnable());
}

class InnerRunnable implements Runnable{    
      @Override    
      public void run() {        
         if(IS_COLLAPSIBLE && COLLAPSIBLE){            
             //可折叠并且已经折叠--文本展开
             tvDesc.setMaxLines(Integer.MAX_VALUE); 
             ivDescOp.setImageResource(R.mipmap.menu_pull_up1);
             COLLAPSIBLE = false;            
             flag = true;        
         }else if(IS_COLLAPSIBLE && !COLLAPSIBLE){            
             //可折叠但未进行折叠--文本折叠            
             tvDesc.setMaxLines(TEXT_MAX_LINE);            
             ivDescOp.setImageResource(R.mipmap.menu_pull_down1);            
             COLLAPSIBLE = true;            
             flag = true;        
         }    
     }
}

5、最后写出一个设置文本内容的方法,让调用者使用。

//设置文本的内容
public void setText(String text){   
    tvDesc.setText(text);    
    requestLayout();
}

</com.collapsibletext.view.CollapsibleTextView>

2、最后一步,要出效果了~!Activity中调用,设置文本就Ok啦
  

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collapsibleTextView = (CollapsibleTextView)
findViewById(R.id.CollapsibleTextView);
//设置文字会出现效果
collapsibleTextView.setText(getResources().getString(R.string.content));
}

* 第一次写东西,有点忐忑。有写得不好的地方,希望可以得到各路大神的批评指正。o((≧▽≦o)

* [代码地址](https://github.com/HappyMiao/CollapsibleText) 
*   QQ 527369301
上一篇下一篇

猜你喜欢

热点阅读