国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-10-22 18:10:15
字體:
來源:轉載
供稿:網友

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類繼承自LinearLayoutpublic class ShowMoreTextView extends LinearLayout {//  用來標記是否為展開狀態  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對象,獲取到布局填充服務    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();  }//  創建setContent方法為TextView填充內容  public void setContent(String content) {    textView.setText(content);  }  public void hideOrShow() {    button.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {      //由hideOrShow的值確定按鈕和textview的狀態        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    ");  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴楚县| 南丹县| 资中县| 诸暨市| 林甸县| 玉田县| 正镶白旗| 石首市| 乐至县| 漯河市| 韩城市| 孟连| 天津市| 巢湖市| 萨迦县| 儋州市| 会昌县| 六盘水市| 临猗县| 台州市| 安岳县| 任丘市| 广宗县| 墨玉县| 井陉县| 陈巴尔虎旗| 天门市| 彭泽县| 定州市| 万宁市| 上蔡县| 大兴区| 达拉特旗| 松江区| 竹北市| 苏尼特左旗| 陇西县| 邓州市| 嫩江县| 武宣县| 彭泽县|