您的位置:首页 > 其它

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: