亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義TextView仿微信朋友圈文字展開全文功能

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

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

代碼及注釋如下:

首先寫一個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 {
//  用來標(biāo)記是否為展開狀態(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);
//    實例化layoutInflater對象,獲取到布局填充服務(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方法為TextView填充內(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;
        }
      }
    });
  }
}

接下來就可以引用了,與普通的控件一樣 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>

測試類:

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
    ");
  }
}

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

相關(guān)文章

  • Android 處理空列表的方法(必看篇)

    Android 處理空列表的方法(必看篇)

    下面小編就為大家?guī)硪黄狝ndroid 處理空列表的方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 一文帶你了解Android?Flutter中Transform的使用

    一文帶你了解Android?Flutter中Transform的使用

    flutter的強大之處在于,可以對所有的widget進行Transform,因此可以做出非常酷炫的效果。本文就來大家了解一下Transform的具體使用,感興趣的可以了解一下
    2023-01-01
  • Android中shape的自定義藝術(shù)效果使用

    Android中shape的自定義藝術(shù)效果使用

    大家好,本篇文章主要講的是Android中shape的自定義藝術(shù)效果使用,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android自定義控件實現(xiàn)圓形進度CircleProgressBar

    Android自定義控件實現(xiàn)圓形進度CircleProgressBar

    這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)圓形進度CircleProgressBar,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android編程之PopupWindow隱藏及顯示方法示例(showAtLocation,showAsDropDown)

    Android編程之PopupWindow隱藏及顯示方法示例(showAtLocation,showAsDropDown

    這篇文章主要介紹了Android編程之PopupWindow隱藏及顯示方法,結(jié)合實例形式分析了showAtLocation及showAsDropDown方法實現(xiàn)PopupWindow控件隱藏及顯示功能相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • Android MVP模式實戰(zhàn)教程

    Android MVP模式實戰(zhàn)教程

    這篇文章主要為大家詳細介紹了Android MVP模式實戰(zhàn)教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 解決Android加殼過程中mprotect調(diào)用失敗的原因分析

    解決Android加殼過程中mprotect調(diào)用失敗的原因分析

    本文探討的主要內(nèi)容是mprotect調(diào)用失敗的根本原因,以及在加殼實現(xiàn)中的解決方案,通過本文的闡述,一方面能夠幫助遇到同類問題的小伙伴解決心中的疑惑,另一方面能夠給大家提供可落地的實現(xiàn)方案,需要的朋友可以參考下
    2022-01-01
  • Android canvas drawBitmap方法詳解及實例

    Android canvas drawBitmap方法詳解及實例

    這篇文章主要介紹了 Android canvas drawBitmap方法詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 解決Android解析圖片的OOM問題的方法!!!

    解決Android解析圖片的OOM問題的方法!!!

    我們在編寫Android程序的時候經(jīng)常要用到許多圖片,不同圖片總是會有不同的形狀、不同的大小,程序占用了過高的內(nèi)存就容易出現(xiàn)OOM(OutOfMemory)異常。本篇文章主要講訴了解決Android解析圖片的OOM問題,有興趣的可以了解一下。
    2016-11-11
  • Android N 7.0中報錯:android.os.FileUriExposedException的解決方法

    Android N 7.0中報錯:android.os.FileUriExposedException的解決方法

    這篇文章主要給大家介紹了關(guān)于在Android N 7.0中報錯:android.os.FileUriExposedException的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧
    2018-05-05

最新評論