国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android自定義TextView仿微信朋友圈文字展開全文功能

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

2022-02-28 15:20CommyTin Android

這篇文章主要為大家詳細介紹了Android自定義TextView仿微信朋友圈文字展開全文功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

代碼及注釋如下:

首先寫一個xml文件 showmore.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//新建java類繼承自LinearLayout
public 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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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>

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
    ");
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/CommyTin/article/details/51800835

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久久久久久久久大牛 | 亚洲精品免费在线观看视频 | 中文成人在线 | 精品在线看| 久草美女| 日韩中文字幕免费在线播放 | 精品日韩在线 | 国产成人在线播放 | 日韩精品一区二区三区在线 | 亚洲午夜成激人情在线影院 | 久久午夜综合久久 | av免费观看网站 | 欧洲一区二区在线观看 | 日韩亚洲一区二区 | 日韩精品久久 | 天天操天天干天天插 | 在线色网 | 久久99精品久久久久久园产越南 | 免费观看的av | 久草视频在线观 | 久久精品国产亚洲一区二区三区 | 欧美精品v国产精品v日韩精品 | 精精国产xxxx视频在线野外 | 亚洲精品一区在线观看 | 欧美一区免费 | 成年人激情视频 | 欧美在线亚洲 | 欧美一区二区在线播放 | 亚洲精品视频在线 | 久色视频在线 | 久久久久国产精品 | 激情自拍偷拍 | 黑人精品 | 99久久精品一区二区成人 | 亚洲欧美成人a毛片 | 在线播放亚洲 | 国产午夜精品美女视频明星a级 | 久久久国产99久久国产一 | 九一视频在线观看 | 中文字幕在线第一页 | 欧美日韩在线视频观看 |