andriod 动态设置TextView 和 RelativeLayou 高度
2014-11-24 15:29
253 查看
XML布局 <RelativeLayout android:id="@+id/rlay_meeting_contact_context" android:layout_width="match_parent" android:layout_height="44dp" android:clickable="false" android:focusable="false"> <TextView android:id="@+id/tv_test" style="@style/B4_Font_white" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/TextView07" android:duplicateParentState="true" android:gravity="left|center" android:singleLine="true" android:text="@string/accept_invite_this_troop"/> </RelativeLayout>
注:android:gravity="left|center"//表示文本从左的中间开始,不然展开后文本会在中显开始显示
JAVA
定义:
privateintmContextHeght=0;
privateBooleanmLookContextflag=false;
privateRelativeLayoutmrlayMeetingContactContext;
mrlayMeetingContactContext=(RelativeLayout)findViewById(R.id.rlay_meeting_contact_context);
privateTextViewmtvTest;
mtvTest=(TextView)findViewById(R.id.tv_test);
利用timer计时器获取:LineCount
注:getLineCount注意:需要待对象展开后才能正确获取,不然获取到的是0
TimerTasktask=newTimerTask(){
publicvoidrun(){
Messagemessage=newMessage();
message.what=1;
handler.sendMessage(message);
}
};
finalHandlerhandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
switch(msg.what){
case1:
android.util.Log.e("Timer","Timer");
update();
break;
}
super.handleMessage(msg);
}
};
privatevoidupdate(){
intlinecount=mtvTest.getLineCount();
Log.i("tv.getLineCount()",mtvTest.getHeight()+"");
if((!mLookContextflag)||(linecount<2)){
mLookContextflag=true;
mtvTest.setEllipsize(null);//展开
mtvTest.setSingleLine(false);
}elseif((mLookContextflag)||(linecount>1)){
mLookContextflag=false;
if(mtvTestt.getLineCount()>1){
intiHeght=mtvTest.getLineCount()
*mtvTest.getLineHeight();
if(iHeght>mContextHeght){
android.view.ViewGroup.LayoutParamspp=mrlayMeetingContactContext
.getLayoutParams();
pp.height=iHeght;
mrlayMeetingContactContext.setLayoutParams(pp);
mMeetingContactContext.setHeight(iHeght);
//timer用完结束掉
if(mTimer!=null){
mTimer.cancel();
mTimer=null;
}
}
}
}
调用:
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
switch(v.getId()){
caseR.id.meeting_contact_context:
if(mContextHeght==0){
mContextHeght=mrlayMeetingContactContext.getHeight();
if(mTimer==null){
mTimer=newTimer(false);
mTimer.schedule(task,10,100);
}
}
}
相关文章推荐
- 动态设置TextView的高度,就是点击收起和展示更多的效果
- 动态设置TextView高度和宽度
- 动态设置TextView的高度
- 根据文本内容的长度动态的设置TextView的高度
- 如何使用代码动态的获取和设置ImageView的宽度和高度?
- TextView lines设置为4行,却只显示2行,但是高度确实4行(已找到原因)
- andorid,动态设置TextView文本中字体的颜色和格式,用Html.fromHtml方法
- android 的TextView空间动态设置字体颜色
- SpannableString 动态设置textView中字体颜色和大小
- 动态布局,并使用代码为TextView设置drawableLeft
- Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用
- Textview动态设置颜色问题
- 动态的获取和设置ImageView的宽度和高度
- iOS 根据文本内容为TextView动态定义高度
- 几种设置UITableView的cell动态高度的方法
- [Android UI界面] 如何在activity中动态的设置TextView上字体的颜色
- 如何动态的获取和设置ImageView的宽度和高度?
- 为TextView设置两种状态,程序中可以动态切换
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
- andriod中checkbox图标的修改、textView(editText)周围图标的位置设置