Android在onCreate()方法中動態獲取TextView控件的高度


正好朋友項目里遇到了給寫了個小Demo:

這個監聽器看名字也知道了。就是在繪畫完畢之前調用的,在這里面能夠獲取到行數。當然也能夠獲取到寬高等信息



package com.example.textviewtest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView text;
	private Button button;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (TextView) findViewById(R.id.text2);
		button = (Button) findViewById(R.id.button);
		text.setText("廣西新聞網-南國今報柳州訊 一消費者到發廊美發,因對美發效果不滿。索要數千至1萬元賠償,並經工商調解不成,進而大"
				+ "鬧發廊騷擾店主,並驚動了警方。警方介入耐心做工作,消費者在警方及工商見證后,接受店主提出的賠償方案,兩方化干戈為玉帛");
		//獲取視圖樹的全局事件改變時得到通知
		ViewTreeObserver vto = text.getViewTreeObserver();
		//監聽獲取回掉函數
		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				//獲取text View 的高度
				int lineCount = text.getLineCount();
				System.out.println(lineCount);
				//邏輯推斷。假設大於2顯示按鈕,假設行數小於或者等於2 則隱藏。

if(lineCount>2){ button.setVisibility(View.VISIBLE); }else{ button.setVisibility(View.GONE); } return true; } }); button.setOnClickListener(new OnClickListener() { Boolean flag = true; @Override public void onClick(View arg0) { // TODO Auto-generated method stub Log.i("zkk", text.getHeight() + ""); if (flag) { flag = false; text.setEllipsize(null);// 展開 text.setSingleLine(flag); button.setText("隱藏"); } else { flag = true; text.setMaxLines(2);// 收縮 button.setText("顯示"); // text.setEllipsize(TruncateAt.END); } } }); } }


以下是布局界面


<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=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1" 
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
             />
        

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="-10dp"
            android:text="下拉" />
    </RelativeLayout>

</RelativeLayout>



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM