最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義TextView仿微信朋友圈文字展開(kāi)全文功能

 更新時(shí)間:2018年06月02日 12:39:21   作者:CommyTin  
這篇文章主要為大家詳細(xì)介紹了Android自定義TextView仿微信朋友圈文字展開(kāi)全文功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android自定義TextView仿微信朋友圈文字信息,展開(kāi)全文功能

代碼及注釋如下:

首先寫(xiě)一個(gè)xml文件 showmore.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/content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:maxLines="3"
    android:ellipsize="end"/>
  <TextView
    android:id="@+id/hide_show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_showmore"
    android:textColor="@color/colorBlue"
    android:textSize="16sp"
    android:text="顯示更多"
    android:padding="0dp"
    />
</LinearLayout>
//新建java類繼承自LinearLayout
public class ShowMoreTextView extends LinearLayout {
//  用來(lái)標(biāo)記是否為展開(kāi)狀態(tài)
  private int hideOrShow = 0;
  private TextView textView;
  private TextView button;

  public ShowMoreTextView(Context context) {
    super(context);
  }

  public ShowMoreTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
//    實(shí)例化layoutInflater對(duì)象,獲取到布局填充服務(wù)
    LayoutInflater layoutInflater = (LayoutInflater)context.
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    填充自定義的布局xml文件
    layoutInflater.inflate(R.layout.showmore,this);
    textView = (TextView)findViewById(R.id.content);
    button=(TextView) findViewById(R.id.hide_show);
    button.setText("顯示更多");
//    隱藏或顯示
    hideOrShow();
  }
//  創(chuàng)建setContent方法為T(mén)extView填充內(nèi)容
  public void setContent(String content) {
    textView.setText(content);
  }

  public void hideOrShow() {
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
      //由hideOrShow的值確定按鈕和textview的狀態(tài)
        if (hideOrShow == 0) {
          button.setText("收起");
          textView.setMaxLines(100);
          hideOrShow = 2;
        }else if(hideOrShow==2){
          button.setText("顯示更多");
          textView.setMaxLines(3);
          hideOrShow = 1;
        }else if(hideOrShow==1){
          button.setText("收起");
          textView.setMaxLines(100);
          hideOrShow=2;
        }
      }
    });
  }
}

接下來(lái)就可以引用了,與普通的控件一樣 activity_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.commy.activity.TestActivity">

  <com.yunfeng.laojiahenan.view.ShowMoreTextView
    android:id="@+id/showmore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

測(cè)試類:

public class TestActivity extends AppCompatActivity {
  private ShowMoreTextView showMoreTextView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    showMoreTextView=(ShowMoreTextView)findViewById(R.id.showmore);
    showMoreTextView.setContent("
      There is so much life
      I've left to live
      And this fire's burning still
      When I watch you look at me 
      I think I could find a way
      To stand for every dream
      And forsake this solid ground
      And give up this fear within
      Of what would happen if they end you
      I'm in love with you
    ");
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

丰镇市| 武山县| 宣化县| 云霄县| 临夏县| 济南市| 和龙市| 阜康市| 崇州市| 西丰县| 榕江县| 鄱阳县| 揭阳市| 辉县市| 泊头市| 赤壁市| 资源县| 临桂县| 镇江市| SHOW| 巧家县| 新沂市| 古浪县| 张家港市| 邮箱| 扶余县| 宁晋县| 原阳县| 南康市| 祁门县| 宜都市| 漳平市| 株洲市| 峨山| 晋江市| 广丰县| 额尔古纳市| 方山县| 洛扎县| 闽清县| 巢湖市|