码迷,mamicode.com
首页 > 其他好文 > 详细

andriod 动态设置TextView 和 RelativeLayou 高度

时间:2014-11-24 16:53:44      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   io   ar   color   sp   java   on   

 1 XML布局
 2             <RelativeLayout
 3                 android:id="@+id/rlay_meeting_contact_context"
 4                 android:layout_width="match_parent"
 5                 android:layout_height="44dp"
 6                 android:clickable="false"
 7                 android:focusable="false" >
 8 
 9                 <TextView
10                     android:id="@+id/tv_test"
11                     style="@style/B4_Font_white"
12                     android:layout_width="match_parent"
13                     android:layout_height="match_parent"
14                     android:layout_alignParentTop="true"
15                     android:layout_toRightOf="@+id/TextView07"
16                     android:duplicateParentState="true"
17                     android:gravity="left|center"
18                     android:singleLine="true"
19                     android:text="@string/accept_invite_this_troop" />
20 
21             </RelativeLayout>

注:android:gravity="left|center"//表示文本从左的中间开始,不然展开后文本会在中显开始显示

 

JAVA

定义:

private int mContextHeght = 0;
private Boolean mLookContextflag = false;

private RelativeLayout mrlayMeetingContactContext;
mrlayMeetingContactContext = (RelativeLayout) findViewById(R.id.rlay_meeting_contact_context);

 

1 private TextView  mtvTest;
2 
3 mtvTest= (TextView) findViewById(R.id.tv_test);

 

利用timer计时器获取:LineCount

注:getLineCount注意:需要待对象展开后才能正确获取,不然获取到的是0

 

 1     TimerTask task = new TimerTask() {
 2         public void run() {
 3             Message message = new Message();
 4             message.what = 1;
 5             handler.sendMessage(message);
 6         }
 7     };
 8 
 9     final Handler handler = new Handler() {
10         public void handleMessage(Message msg) {
11             switch (msg.what) {
12             case 1:
13                 android.util.Log.e("Timer", "Timer");
14                  update();
15                 break;
16             }
17             super.handleMessage(msg);
18         }
19 
20     };
21 
22     private void update() {
23         int linecount = mtvTest.getLineCount();
24 
25         Log.i("tv.getLineCount()", mtvTest.getHeight() + "");
26         if ((!mLookContextflag) || (linecount < 2)) {
27 
28             mLookContextflag = true;
29             mtvTest.setEllipsize(null); // 展开
30             mtvTest.setSingleLine(false);
31         } else if ((mLookContextflag) || (linecount > 1)) {
32             mLookContextflag = false;
33             if (mtvTestt.getLineCount() > 1) {
34                 int iHeght = mtvTest.getLineCount()
35                         * mtvTest.getLineHeight();
36 
37                 if (iHeght > mContextHeght) {
38                     android.view.ViewGroup.LayoutParams pp = mrlayMeetingContactContext
39                             .getLayoutParams();
40                     pp.height = iHeght;
41                     mrlayMeetingContactContext.setLayoutParams(pp);
42                     mMeetingContactContext.setHeight(iHeght);
43                     //timer用完结束掉
44                     if (mTimer != null) {
45                         mTimer.cancel();
46                         mTimer = null;
47                     }
48                 }
49 
50             }
51 
52         }

 

调用:

 1     @Override
 2     public void onClick(View v) {
 3         // TODO Auto-generated method stub
 4         switch (v.getId()) {
 5         case R.id.meeting_contact_context:
 6             if (mContextHeght == 0) {
 7                 mContextHeght = mrlayMeetingContactContext.getHeight();
 8                 if (mTimer == null) {
 9                     mTimer = new Timer(false);
10                     mTimer.schedule(task, 10, 100);
11                 }
12             }
13 }

 

andriod 动态设置TextView 和 RelativeLayou 高度

标签:android   style   blog   io   ar   color   sp   java   on   

原文地址:http://www.cnblogs.com/jenson138/p/4118803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!